How to export simulink data to workspace during simulation?

I want to get data from simulink during simulation and use the serial network function to send this data to another program. Because I need to use another program to perform some tricks and send the command back to simulink, so I need to get data from simulink at runtime so that another program can make the correct command.

I tried using the Workspace block to export data.

enter image description here

However, I can only get value at the very beginning of the simulation.

And I also tried to use the scope and change some properties: check the Save data to workspace checkbox and clear the Limite data from Last.

enter image description here

enter image description here

First, I started modeling, and I found that ScopeData is not displayed in the workspace. Only when I stop modeling will ScopeData appear in the workspace.

enter image description here

And after that I can use ScopeData.signals.values ​​to get the values.

But I want: when I start the simulation, ScopeData will appear in the workspace so that I can send this data to another program.

Does anyone know how to achieve this?

I found this page to be useful, but I still don't know how to constantly export data during the simulation.

+7
source share
3 answers

Use get_param to read data only at the current time. Also send data back to Simulink using set_param gain or another block.

get_param example

First download and run the simulation:

 load_system('myModel') set_param('myModel','SimulationCommand','Start'); 

To read data on any line of your simulink model:

  • Get the simulink block object (try Clock named Clock):

     block = 'myModel/Clock'; rto = get_param(block, 'RuntimeObject'); 
  • Then get the data on your first (or any) output port (or input) of this block.

     time = rto.OutputPort(1).Data; 

You can read in the timer callback.

It may also be useful: Command line functionality for Simulink

+10
source

During the simulation, Simulink stores the recorded data in the internal buffer and only writes data to the Workspace when the simulation is paused or stopped. It sounds as if you really need to write an S-function (which will receive the signal values ​​by time in time) and communicate with Proteus in this way.

Of course, Simulink is a real-time simulator, so if you are talking about doing something similar to real-time control, you are most likely not completely wrong.

+3
source

At any time during the simulation, you can force Simulink to write the output of the simulation to the workspace:

 set_param(bdroot,'SimulationCommand','WriteDataLogs'); 

I found that this command is rather unstable in my Matlab 2010a for Win64. In particular, I should avoid this when the simulation is stopped (i.e., the first get_param(bdroot,'SimulationStatus') check get_param(bdroot,'SimulationStatus') ), otherwise Matlab shows an error and asks to restart.

+2
source

All Articles