MATLAB Answers

How can I update a GUI with values from my Simulink model as it is running by using a Execution Event Listener?

126 views (last 30 days)
I have a Simulink model and I would like to display the value of the output of certain blocks in my GUI.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 7 Sep 2015
There is no direct way to access the runtime parameters of a Simulink model from a MATLAB GUI without the use of some callback function. To work around this issue, you need to create a function that will update the GUI with the desired model data at each time step. An outline of this procedure is as follows.
NOTE: This solution is specific to normal mode simulation. It does not work when the model is run in accelerator mode or built into an executable. In these cases, one way to get the model to work is to write the value to a text file in the model, and then setup a timer to read the value from the text file in the GUI.
You can download the attached model and GUI to see a working example.
1. Create a GUI with a control that you would like to update with information from the model. In this case we would like to see the output of the model's Gain block in the GUIs edit text window.
2. Create a model callback function that will register an event listener for the block in question. This function will be called every time the block is updated. We will use it to access the value of the Gain blocks output and pass them to the GUI.
In this case we will use the models 'StartFcn' callback.
%The GUI handles are by default hidden, turn them on
%Set up the arguments that will go into the gain block event callback listener
blk = 'mytestmdl/Gain';
event = 'PostOutputs';
listener = @updategui;
%Create the listener
h = add_exec_event_listener(blk, event, listener);
3. Create a MATLAB file function that will get the Gain block's runtime output parameter and pass it to the GUI.
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
You can try out an example by following the steps below.
1. Download the attached files
2. Open the model file.
3. Open the GUI by running the file 'mytestgui.m'
4. Start the simulation by clicking the 'Start' button in the GUI.
5. You can change the value of 'Gain' in the GUI while the simulation is running.
6. Click 'Stop' in the GUI to halt the simulation.


Sign in to comment.

More Answers (3)

maitham dib
maitham dib on 6 Feb 2017
How do I adapt the updategui.m for a plot on an axes?


Sam Trossman
Sam Trossman on 12 May 2020
You could start with one of the following approaches.
Assume you first initialize an empty plot:
t = []; % Time stamps
simData = []; % Block output data from Simulink
h = plot(t,simData) % Initialize plot and store handle "h"
In updategui.m, extract the data from the "RuntimeObj" as before, but this time without the string conversion.
rto = get_param('mytestmdl/Gain','RuntimeObject');
currentPoint = rto.OutputPort(1).Data;
Add the new data to "simData" and update the plot.
simData = [simData currentPoint]; % Append the newest data point
h.YData = simData; % Update the "YData" property of the plot handle
drawnow; % Update the visualization
Alternatively, you could follow this example:
where you would set the "YDataSource" to "simData" initially and avoid "h.Ydata = simData" as before. You would also need to add "refreshdata" in this case.
Note that either of these approaches could be further optimized by pre-allocating the size of the "simData" vector before simulation.
============================== Other references ==============================
  1. You should be able to follow a similar process for a "UIAxes" in an App Designer app:
  2. General "animation" techniques:

Sign in to comment.

Babak on 19 Dec 2013
do you have any idea why this errro comes runing the mytestmdl.mdl?
Attempt to execute SCRIPT varargin as a function: C:\Program Files\MATLAB\R2012b\toolbox\matlab\lang\varargin.m

Gregor Safronov
Gregor Safronov on 4 May 2016
Hi. Can you please advice-i use the same method to collect data from Simulink model. In attached file current data is only shown in Edit. But i need to make an array.Other words, i have to put each updated value from simulink model(this one - rto = get_param('mytestmdl/Gain','RuntimeObject');) in the next cell of array. Thanks.


Sign in to comment.




Community Treasure Hunt

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

Start Hunting!