8

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.

Po-Jen Lai
  • 381
  • 1
  • 7
  • 18
  • Are you sending the data to a Matlab program or a different program like Excel or something? If so, what other program? – Tyler Durden Jun 09 '13 at 02:49
  • I am sending data to Proteus, because I want to write some commands on microchip to control the blocks in simulink. – Po-Jen Lai Jun 09 '13 at 03:16
  • I know that you can get the data on the simulink lines using [`get_param`](http://www.mathworks.com/help/simulink/slref/get_param.html) cammand when the simulink is set to **external mode** during the simulation (not sure about other modes). You can set up a timer (with the same frequency of your simulink) and do this in its `callback`. – p8me Jun 09 '13 at 03:51
  • @pm89 Thanks~this is a way that I can try! – Po-Jen Lai Jun 09 '13 at 03:56

3 Answers3

11

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

p8me
  • 1,808
  • 1
  • 14
  • 21
  • 3
    Although this would get you some data, the timer is working in MATLAB and has nothing to do with the Simulink simulation time, so there's no guarantee of getting data at the simulation times you may need. – Phil Goddard Jun 09 '13 at 05:33
  • I do agree with you @PhilGoddard, but I found this to be the only way. There are however some workarounds and dirty tricks to prevent missing any data, like increasing the timer frequency (to reduce the chance of loosing any data) and setting up a counter in simulink (to prevent getting the same data twice and scanning if any data is lost or not). – p8me Jun 09 '13 at 15:26
  • The workarounds do not prevent you missing data. They may assist in missing less data, but they cannot guarantee that you miss no data. – Phil Goddard Jun 09 '13 at 15:55
3

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.

Phil Goddard
  • 10,286
  • 1
  • 13
  • 25
  • I want to control a robot arm in simulink(simmechanics, actually) using microchip in Porteus as controller. So is this not achievable? I don't understand the meaning of Simulink is a non-realtime simulator~ – Po-Jen Lai Jun 09 '13 at 03:55
  • 2
    None of the OS's that MATLAB/Simulink run on are real-time. Amongst other things this means that 1 unit of simulation time is (almost certainly) not equal to 1 unit of real-time. The clock in Simulink may say it's been running for 10s, but the clock on your wall may say something less than 10s or something longer than 10s (depending on the complexity of your model). Unless you only require slow sample rates, and don't care about guaranteed performance, then for real-time control you need to convert the model to C (using Simulink Coder) and then compile and run that code on a real-time OS. – Phil Goddard Jun 09 '13 at 05:24
  • You can always test your logic with simulink streaming live data to a controller, just put in consideration, that it is not real time. The main logic can be tested, but critical timings tasks will not be possible (e.g. the reaction time in microseconds of an event that happens over interrupt or something equivalent) – Karim Apr 29 '14 at 09:53
3

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.

Sandro
  • 31
  • 1