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

143 次查看(过去 30 天)
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.

回答(5 个)

Jarrod Rivituso
Jarrod Rivituso 2011-3-23
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!

Kaustubha Govind
Kaustubha Govind 2011-3-22
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 2011-3-22
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 2011-3-22
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 个评论
Jarrod Rivituso
Jarrod Rivituso 2011-3-23
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')

请先登录,再进行评论。


Szabolcs Pethe
Szabolcs Pethe 2011-3-23
Thanks for all of you guys, I hope any of that will fit to my system. :)

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by