How to store output data from a simulink model to a text file

47 次查看(过去 30 天)
Hi!
I have a simulink model with several output signals in a bus. I'm monitoring one of the signals and I would like to store the time stamp when the signal is activated. This will only happen a few times during a 10 min long simulation with logged data. After the simulation has finished I would like to create a text-file containing the stored time stamps. I haven't worked with simulink for so long so I have no idea how to do this in an effective way! Thank you in advance!
Erik

回答(3 个)

Fangjun Jiang
Fangjun Jiang 2011-9-16
There is no way available to write to a text file directly in Simulink. You can use the "To Workspace" block or "To File" (a .mat file) to store whatever data.
Then with those data, you can use save() in Command Window to save the data to a text file.
save File Data -ascii
  2 个评论
Erik
Erik 2011-9-16
Thank you for the answer! Is there some easy way to only store data to a mat-file when the signal is active?, in order to reduce the size of the mat-file for long simulations.
Fangjun Jiang
Fangjun Jiang 2011-9-16
Humh, that's a good question. When you say the signal is inactive, do you mean the signal is zero, or the signal is not even calculated.
If the signal is not even calculated, you must have those calculation inside a triggered or enabled system. You can try to put the "To Workspace" or "To File" block also inside that system to see how it works. I never used it that way before.
If the signal is zero, then I would say the zero signal still gives you valuable information. You should still record it. If you don't want to save all those zeros to a text file, you can do post-processing after the simulation and before writing to the text file. For example: Data below is your data, first column is time and second column is value:
Data=[1,0;2,1;3,0;4,0;5,0;6,1;7,0;8,0]
NewData=Data(Data(:,2)~=0,:)

请先登录,再进行评论。


Erik
Erik 2011-9-19
Thank you for your answer! Sorry I didn't expalin to well! The signal is either zero or a square wave with value 1. I stored all values during the simulation and filtered the data after and it works fine!
I have another question you might be able to answer. One other signal I would like to monitor is boolean. What I would like to do is to save the current time when this signal is true. I don't think it's possible to use save to file with save format Array.
  1 个评论
Fangjun Jiang
Fangjun Jiang 2011-9-19
You are right. You need to use the "To Workspace" block and set the "save format" to be "structure with time". Then you can use save() in the Command Window to save the data to a .mat file.

请先登录,再进行评论。


Tung
Tung 2011-9-26
Hi Fangjun Jiang.
I think there is another way to write to a text file directly in Simulink that we add a function block with the code use the fprintf command. This is possible?
  1 个评论
Fangjun Jiang
Fangjun Jiang 2011-9-26
That's possible. You can try it and report it back. However, it will be inefficient and awkard. Simulation runs at every time step. You approach will require hard drive access at every time step, maybe even open and close the file at every time step.

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by