Get value from simulink model to matlab gui while simulink is running

With respect to following post I have some doubts related to command get_param:
1) When I use this command it returns me empty vector. 2) Can we use this command for any block like display, demux and out or it has to be Gain block only?
Thanks Mitesh

 Accepted Answer

I believe it can be done that way and it should apply to all types of blocks. Remember you can't just use get_param() alone, you need to set up the event listener and callback function. See Guy Rouleau's anser in this post. It points to the link from the Mathworks website.

15 Comments

but when I use different type of block instead of 'Gain' block, it's giving me this error
"add_exec_event_listner can add listener only when block diagram is executing"
I am not sure. What type of block are you trying? I guess it may not apply to some virtual blocks such as Mux, DeMux, BusCereator, BusSelector because they could be "virtual", which means they are for graphical reason and they are non-exist when the simulation is run.
I have tried; Out, display, subsystem, scope, hold and Toworkspace blocks. Could you please suggest me any block other than Gain which I can use to get or read the output.
Thanks
Some block that has a math operation, such as Sum, Prod, etc.
Hi Fangiun,
I have one more concern regarding this; can we get update from multiple block in a single function call. Like
function varargout = updategui(varargin)
%create a run time object that can return the value of the gain block's
%output and then put the value in a string.
rto = get_param('mytestmdl/Gain','RuntimeObject');
str = num2str(rto.OutputPort(1).Data);
%get a handle to the GUI's 'current state' window
statestxt = findobj('Tag','curState');
%update the gui
set(statestxt,'string',str);
%%
rto2 = get_param('mytestmdl/Gain2','RuntimeObject');
str2 = num2str(rto2.OutputPort(1).Data);
%get a handle to the GUI's 'current state' window
statestxt2 = findobj('Tag','curState2');
%update the gui
set(statestxt2,'string',str2);
If we call multiple blocks in a common function and update them on GUI they all are reading same value. Is there any way to differentiate them?
Thanks
I am not sure. Although the function is triggered by the event (e.g. update output) of block Gain, the function should be able to get the output value of block Gain2. Although that value might not be the updated value at the same time step, they should not simply be the same value as block Gain. Put a break point at the end of this updategui() function and check the value of rto and rto2. They should be different. Also, purposely make the output of Gain and Gain2 vastly different.
value from both gain blocks are way different, one is pressure variable and other is temperature variable. I checked with break point, value for both str and str2 are different in mfile but on GUI they are updating as same numbers on two different edit text windows.
I have 9 varibles on GUI and all are reading one same value.
Okay then, it's not a Simulink runtime object problem. It's a problem of the GUI programming?
Because your computer is kind of busy running the simulation, could you try to add a line drawnow at the end of your updategui() function to re-fresh the GUI?
I tried drawnow, still it is behaving the same. I can see values getting updated, numbers are changing in edit box but they all read the same number. One more thing, can we control the update rate in GUI window. For example if I want to update any particular variable after every 2 sec. Updating variables at different rates might solve this problem.
I am confused by "numbers are changing in edit box but they all read the same number". Can you try to separate these two problems, Simulink runtime object problem or GUI problem? Can you print out Simulink runtime object data in Command Window? Can you try a simple GUI with other data?
Regarding update rate, I think that is determined by the event based on your current implementation. You added an event listener, right?
I suggest you create a new question once you figure out the underneath problem. The comments here are going to be buried.
I am sorry for that confusion. I will try to rephrase my problem and will put it as new question.
Yes I did create a event listner. So we can not chnage the update rate of event listner?
My understanding is, whenever the event happened, the pre-defined callback function will be executed.

Sign in to comment.

More Answers (3)

  1. When you use get_param where? what block and what code?
  2. That command can only get the parameters supported by the block you are using as argument.

5 Comments

In my gui I am using this syntax to get the updated value from simulink model
get_param('mdl/blkname','RuntimeObject')
GUI starts the simulink model and after that I want the updated(calculated) value from simulink model to be displayed on GUI. Values in simulink models are updating in a display block.
That only gives the handle to the object, you should use listeners instead, please see this:
http://www.mathworks.com/matlabcentral/fileexchange/24294-simulink-signal-viewing-using-event-listeners-and-a-matlab-ui
Thanks got it. Earlier my listener was not responding properly.
but when I use different type of block instead of 'Gain' block, it's giving me this error
"add_exec_event_listner can add listener only when block diagram is executing"
You first have to start simulation, and right after that you can add eventlisteners.

Sign in to comment.

Another solution would be to use a Matlab function in your model to update the GUI handle in each timestep. Pass the value from model which needs to be displayed in GUI and update GUI handle respectively.
Another posible solution is using another block instead of a "Gain" block. i used a "Product" and worked like it was a "Gain" block. rto = get_param('lm35_sim/Product','RuntimeObject');

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!