Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export simulink data to workspace during simulation?

I want to retrieve the data from simulink during simulation, and use serial network function to send these data to another program. Because I need to use another program to do some tricks and send command back to simulink, so I have to get data from simulink during runtime so that another program can make the right command.

I've tried using To Workspace block to export the data.

enter image description here

However, I can only got value in the very beginning of the simulation.

And I've also tried using scope and change some properties: check Save Data To Workspace and Uncheck Limite data to Last.

enter image description here

enter image description here

First, I started simulation, and I found the ScopeData didn't appear in the Workspace. Only when I stop simulation, ScopeData would appear in workspace.

enter image description here

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

But what I want is: when I start simulation, ScopeData would appear in workspace so that I can send these data to other program.

Does anyone know how to achieve this?

I found this page might be helpful, but I still don't know how to continuously export data during simulation.

like image 722
Po-Jen Lai Avatar asked Jun 09 '13 02:06

Po-Jen Lai


People also ask

How do I get data from Simulink to workspace?

On the Modeling tab, under Settings, click Model Settings. Then, in the Configuration Parameters dialog box, select Data Import/Export and select Single simulation output. You run a set of simulations using the Multiple Simulations pane. You simulate the model programmatically using one or more Simulink.

Can you export data from Simulink to MATLAB workspace?

You can use a To Workspace (Simulink) block, from the DSP System Toolbox™/Sinks library to send data to the MATLAB workspace as a vector. For example, you can send the error rate data from the Hamming code model, described in the section Reducing the Error Rate Using a Hamming Code.

Can Simulink write to MATLAB workspace?

Description. The Signal To Workspace block writes data from your simulation into an array or structure in the main MATLAB® workspace. You can specify a name for the workspace variable as well as whether the data is saved as an array, structure, or structure with time.


3 Answers

Use get_param to read data from just at the current time. Also to send the data back to Simulink with set_param of a gain or another block.

An example of get_param

First load and start the simulation:

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

To read data on any line of your simulink model:

  1. Get a simulink block object (let's try a Clock with the name Clock):

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

    time = rto.OutputPort(1).Data;
    

You could do the reading, in a timer callback.

Also this might be helpful: Command Line Functionality for Simulink

like image 138
p8me Avatar answered Nov 15 '22 19:11

p8me


During simulation Simulink stores logged data in an internal buffer and only writes the 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 get signal values on a timestep-by-timestep basis) and communicate with Proteus that way.

Of course Simulink is a non-realtime simulator, so if you are talking about doing anything resembling real-time control then you are most likely taking the wrong approach altogether.

like image 37
Phil Goddard Avatar answered Nov 15 '22 19:11

Phil Goddard


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

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

I've found that this command is quite unstable in my Matlab 2010a for Win64. In particular I have to avoid it when simulation is stopped (i.e. first check get_param(bdroot,'SimulationStatus') ), otherwise Matlab shows an error and asks to be restarted.

like image 45
Sandro Avatar answered Nov 15 '22 21:11

Sandro