MATLAB Answers

How can I save data to a csv file from a simulation in simulink without use the Matlab workspace?

147 views (last 30 days)
Szabolcs Pethe
Szabolcs Pethe on 21 Mar 2011
I have a simulation, and now I can write a csv file only if I use a Scope and save the data from that to csv file via Matlab workspace.
These are my tests:
csvwrite(’test.csv’,ScopeData5) dlmwrite(’test.csv’, ScopeData5, ’;’) dlmwrite(’test.txt’, ScopeData5, ’;’)
But if it's possible, I want to save my datas directly from my simulink model.
Thanks for any advice.

Answers (5)

Kaustubha Govind
Kaustubha Govind on 22 Mar 2011
There is no way to directly export simulation results to a CSV file, but you can use the "To File" block to log to disk as a MAT-file. (The MAT-file can be used directly or the data can be rewritten into a CSV later)

Jarrod Rivituso
Jarrod Rivituso on 22 Mar 2011
Since you used the word "possible", I feel inclined to mention that you can write an S-function in C that could do this.
I know a guy that put an example of such a thing on the file exchange... :)
Of course, this is a more advanced approach.

Szabolcs Pethe
Szabolcs Pethe on 22 Mar 2011
I'm a real rookie in Simulink and Matlab, so I don't know what to do. :) I will look for any solution with this S-Function, I hope I can do it finally. This .mat thing: If I just erase the "mat" extension, and write it to "csv", it would work? Is that what you think about? :)
  1 Comment
Jarrod Rivituso
Jarrod Rivituso on 23 Mar 2011
You cannot just erase the .mat extension and replace it with .csv. The .mat is a binary file format, very different from .csv.
However, you can easily load and save data from .mat files in MATLAB using the load and save functions. For example..
>> load('mydata.mat')

Sign in to comment.

Jarrod Rivituso
Jarrod Rivituso on 23 Mar 2011
If you are just looking to have the .CSV file created by the end of the simulation, then you could use the model StopFcn to perform the save.
The steps would be...
1. Allow the model to write the data to the MATLAB workspace. For this, you can use the logged data from the Simulink scopes, as you have shown. Also, there are many other methods for capturing data from a simulation, such as logging signal data (right-click the signal and select Signal Properties) or saving root-level Outport data (this typically shows up in a yout variable in your workspace).
2. Add the csvwrite code to your model's StopFcn. To do this, select File -> Model Properties from the Simulink menu, then select the Callbacks tab, then put your csvwrite code in the StopFcn. The StopFcn gets called immediately after the simulation is over, so you can reference the logged MATLAB workspace variables from there.
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!