Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Stream data form MATLAB to Simulink

Subject: Stream data form MATLAB to Simulink

From: Michal

Date: 22 Nov, 2012 19:59:08

Message: 1 of 7

Hi everyone. My problem is that I need to stream variable from running MATLAB m-code script do running Simulink in real time. I was trying to use "From Workspace" block but, it gathers the value only once at the beginning of the simulation. I cannot just run a m-code script, save data and then transfer it to Simulink becouse both together need to work with real time vision system. How can I solve that problem?

Subject: Stream data form MATLAB to Simulink

From: Phil Goddard

Date: 23 Nov, 2012 22:31:06

Message: 2 of 7


Firstly note that neither MATLAB nor Simulink are real-time applications, so you will never get true real-time using them.

If you want to push data from MATLAB into Simulink you need to use set_param to change the parameter of a block (while the simulation is running).
This will force Simulink to start using the new value.
There a short example of using set_param at,
http://www.goddardconsulting.ca/simulink-creating-using-matlab-code.html

A more typical thing to do would be to write a Simulink block (most likely an m-code S-Function given the info you have given) that check the workspace for new values and uses them.
There are numerous ways of doing that, some much less efficient than others, so you'd need to be careful in choosing the right approach.

Phil.

Subject: Stream data form MATLAB to Simulink

From: Michal

Date: 10 Dec, 2012 11:27:06

Message: 3 of 7

Thank you very much for your answer. I have tried both solutions that you gave to me, but unfortunately both doesn't work for me how I would like to. The main problem is that whatever method I use there is a need to stop MATLAB script for a moment to let Simulink gather a data, which means I have to use pause() command inside of main for loop of my MATLAB script. If I don't do this, Simulink waits with executing a model at t=0. The problem is that a minimum time for which the m-code script can be stopped by pause() command is approximately 0.0156 s. which is to long for me. That script realize object tracking on the video images from webcam, so such a long pauses causes the fps decrease.
So, my next question, more precise is: Is there any way to stream data from running MATLAB script to running Simulink model without, or with just very small delay?

Subject: Stream data form MATLAB to Simulink

From: Phil Goddard

Date: 10 Dec, 2012 18:13:15

Message: 4 of 7

There is absolutely no need to use a pause.

To see this,
- Open a new model
- connect a Constant block to a Display block
- change the final time to inf.
- start the model.

Then from within MATLAB (either the command line or within a file) type
>> set_param('untitled/Constant','Value','30');
and notice that the display changes.
Do the same for other gain values and again see the display change.

Your model is no doubt more complex than this, but the same concept applies.

Phil.

Subject: Stream data form MATLAB to Simulink

From: Michal

Date: 10 Dec, 2012 20:56:16

Message: 5 of 7

Ehh... Sorry, but You still don't get a point... :/ Ok, then, I'll try to show you something:

1. Build a model in Simulink, just as you wanted me to build, and in addition connect a Scope to the output of Constant. Put an 'a' inside a Constant as parameter, and set inf for a time simulation.

2. Create a m-file with a simple code just like this one:

    tic
for i= 1:1000
    a=int2str(i)
    set_param('model1/Constant','Value',a);
end
    toc

3. Set an a for a initial value a=0;
4. Start A Simulink model.
5. Start a m-file script
6. Wait for a script to reach it's end (takes approximately 1.4 sec. on dual core 2 ;) )
7. Stop a Simulink model
8. Open a Scope...

And then you will finally know that the problem is... You see know? If still don't I'll explain one more time: For the beginning the model1 was running with a=0 and displaying that value... Then when you've started a m-file script it pauses... waits until it ends and continue to run with the LAST value of parameter a, which is in that case 1000. I can record a movie if it's still not enough clear what the problem is. ;)

Subject: Stream data form MATLAB to Simulink

From: Michal

Date: 10 Dec, 2012 20:58:11

Message: 6 of 7

You still don't get a point... :/ Ok, then, I'll try to show you something:

1. Build a model in Simulink, just as you wanted me to build, and in addition connect a Scope to the output of Constant. Put an 'a' inside a Constant as parameter, and set inf for a time simulation.

2. Create a m-file with a simple code just like this one:

    tic
for i= 1:1000
    a=int2str(i)
    set_param('model1/Constant','Value',a);
end
    toc

3. Set an a for a initial value a=0;
4. Start A Simulink model.
5. Start a m-file script
6. Wait for a script to reach it's end (takes approximately 1.4 sec. on dual core 2 ;) )
7. Stop a Simulink model
8. Open a Scope...

And then you will finally know that the problem is... You see know? If still don't I'll explain one more time: For the beginning the model1 was running with a=0 and displaying that value... Then when you've started a m-file script it pauses... waits until it ends and continue to run with the LAST value of parameter a, which is in that case 1000. I can record a movie if it's still not enough clear what the problem is. ;)

Subject: Stream data form MATLAB to Simulink

From: Jerry Gregoire

Date: 30 Jan, 2013 18:51:08

Message: 7 of 7

"Michal" wrote in message <ka5id3$hle$1@newscl01ah.mathworks.com>...
> You still don't get a point... :/ Ok, then, I'll try to show you something:
>
> 1. Build a model in Simulink, just as you wanted me to build, and in addition connect a Scope to the output of Constant. Put an 'a' inside a Constant as parameter, and set inf for a time simulation.
>
> 2. Create a m-file with a simple code just like this one:
>
> tic
> for i= 1:1000
> a=int2str(i)
> set_param('model1/Constant','Value',a);
> end
> toc
>
> 3. Set an a for a initial value a=0;
> 4. Start A Simulink model.
> 5. Start a m-file script
> 6. Wait for a script to reach it's end (takes approximately 1.4 sec. on dual core 2 ;) )
> 7. Stop a Simulink model
> 8. Open a Scope...
>
> And then you will finally know that the problem is... You see know? If still don't I'll explain one more time: For the beginning the model1 was running with a=0 and displaying that value... Then when you've started a m-file script it pauses... waits until it ends and continue to run with the LAST value of parameter a, which is in that case 1000. I can record a movie if it's still not enough clear what the problem is. ;)

Phil,
The above sarcasm is unfortunate but the issue is real. I find the following:
1) If I run the code suggested:
tic
for i= 1:10
    a=int2str(i);
    set_param('untitled/Constant','Value',a);
% pause(eps); % A pause is necessary for a running model of Simulink to update
end
toc
 
Simulink model is Constant ---> scope, sample time set to 1ms.


while Simulink is running (to inf), The constant block on the Simulink GUI is not updated unless I pause the mcode. This is typical with ML, (e.g.) plots.

If I put the pause in the mcode while concurrently running the model (0 inf), then things really slow down. 15 - 30 sec for a = 1:10 with pause/update compared to 7ms without. With the pause the scope shows staircase as expected, but it is very uneven, (not expected).

Unfortunately, without the pause the scope only seems to update at the end resulting in a flat trace at 10.

Any understanding of what/how ML and SL are interacting would be helpful. I have seen timing get all messed up if I had concurrent ML timers running. See http://www.mathworks.com/matlabcentral/newsreader/view_thread/323676#890357. Is this the issue with ML and SL? I know Real time behavior can not be expected with ML, but approximate Real Time would be nice. It seems to just fall flat on its face in this area.

Jerry

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us