views:

2349

answers:

2

I have a Simulink model that uses an embedded MATLAB function for a block, and I haven't been able to figure out how to move data between the embedded MATLAB block and a GUI in real-time (i.e. while the model is running). I tried to implement a "to workspace" block in my model but I don't know how to correctly use it.

Does anyone know how to move data from a Simulink block into a GUI in real-time?

A: 

Non-real-time solution:

If you want to set parameters in a GUI, simulate a model with those parameters, and then display the simulation output in the GUI, there is a good tutorial on blinkdagger.com. One solution they describe is using the SIMSET function to define which workspace the Simulink model interacts with. You should be able to supersede the base workspace so that data is instead sent to and from the workspace of the GUI functions that are calling the Simulink model.

Real-time solution

As suggested by MikeT, you can use a RuntimeObject. You first have to use the get_param function to get the RuntimeObject from the block:

rto = get_param(obj,'RuntimeObject');

Where obj is either a block pathname or a block-object handle. You can get the pathname of the most recently selected block using the GCB function (in which case you can replace obj with gcb). You can then get the block's output with the following:

blockData = rto.OutputPort(1).Data

One additional caveat from the documentation:

To ensure the Data field contains the correct block output, turn off the Signal storage reuse option (see Signal storage reuse) on the Optimization pane in the Configuration Parameters dialog box.

You would likely end up with a loop or a timer routine running in your GUI that would continuously get the output data from the RuntimeObject for as long as the simulation is running. The documentation also states:

A run-time object exists only while the model containing the block is running or paused. If the model is stopped, get_param returns an empty handle. When you stop or pause a model, all existing handles for run-time objects become empty.

Your loop or timer routine would thus have to keep checking first that the RuntimeObject exists, and either stop (if it doesn't) or get the data from it (if it does). I'm unsure of exactly how to check for existence of a RuntimeObject, but I believe you would either check if the object is empty or if the BlockHandle property of the object is empty:

isempty(rto)  % Check if the RuntimeObject is empty
%OR
isempty(rto.BlockHandle)  % Check if the BlockHandle property is empty
gnovice
ok, but isnt it possible to set the parameters without using the workspace? i already can set the "gain" block from my gui ... i mean i can set the constant of the block from my gui with a slider. but i dont know how to get the values of the out of my embedded matlab function. there must be something without using workspace
I think there's a communication breakdown happening here. Did you look at the example I linked to on blinkdagger? Is that the sort of thing you are trying to do (set parameters in a GUI, simulate a model with those parameters, then display the simulation output in the GUI)?
gnovice
i guess it is, but i cant open this or i just donno how, i just can see the 52 comments :s
The links are easy to miss. The tutorial has 4 pages. After the intro and before the comments you should see "Pages: 1 2 3 4", with each number as a link to a page of the tutorial.
gnovice
A: 

From your responses, I'm guessing you want to see the results while the simulation is running, is that correct? The blinkdagger.com tutorial lets you view the results of a simulation after it is done, but not while it is running. Do you basically want to embed something like a scope block into your GUI?

There's a few ways to do this, the best is probably using the EML block's runtime object. If you use this, you should be able to look at the output of the EML block while it is running.

MikeT
yes, i want to use the result of the EML while simulation is running. there is an example for the call ... rto = get_param(gcb,'RuntimeObject') ... so the syntax in my case is just rto = get_param(gcb,'name_simulink_block') where name_simulink_block is the name of my EML ???