How to store output data from a simulink model to a text file
38 次查看(过去 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
0 个评论
回答(3 个)
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 个评论
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
2011-9-19
1 个评论
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
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Sources 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!