How do I update a GUI designed in App Designer with data from a running Simulink model?

259 views (last 30 days)
I want to update a GUI that I have designed in App Designer using data that is generated during the simulation of a Simulink model. In other words, how do I access run-time parameters of a Simulink model from App Designer?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Aug 2020
Let the Simulink model being used be named "Trial.slx" and the App Designer app being used be named "TrialApp.mlapp". There are three steps involved in accessing run-time parameters of a Simulink model from App Designer. They are listed below:
1. In the Simulink model callback "InitFcn" function, define:
hApp = TrialApp;
2. In the Simulink model callback "StartFcn" function, call a function named "updateGUI()" as described below. This method will be called every time the specified block's ("blk") output method executes.
blk = 'Trial/DisplayBox';
event = 'PostOutputs';
listener = @(app, event) updateGUI(hApp);
h = add_exec_event_listener(blk, event, listener);
3. Create a public method named "updateGUI()" in the app designed in App Designer. This function will get the specified block's run-time parameters and pass it to the GUI. This function is defined below.
function updateGUI(app, varargin)
% Create an object that gets the run-time value of the specified block
rto = get_param([bdroot,'/DisplayBox'],'RuntimeObject');
% Update the GUI accordingly. In this case a the value of an EditField is being updated.
app.EditField.Value = rto.InputPort(1).Data;
Alireza Ghaderi
Alireza Ghaderi on 23 Aug 2020
I followed your instruction and created a event listener. My simulink model is a real time data gathering from a serial device with the rate of 100Hz. After using this method, My Simulink model is not real time and has a huge delay... Also I didnt get the value in app as well...

Sign in to comment.

More Answers (4)

Serhiy Kapustyan
Serhiy Kapustyan on 23 Feb 2021
Hello the Problem is that if i define a start button and run the simulation, then Gui opens each time the simulation starts! What to do in this case?
Stephen Mascaro
Stephen Mascaro on 26 Jul 2022
The easy answer is you would put it in the updateGUI function. This function will be called every time an event occurs in Simulink. However in practice you don't want to redraw your entire plot every time, or else your App will run too slow. It's better to have an initial plot command somewhere else in your App and then use the updateGUI function to update your plot rather than completely redraw it.

Sign in to comment.

Nancy on 7 Jun 2021
Does it make a difference whether the definition "hApp = TrialApp;" goes int InitFcn or in StartFcn? I'm not clear on what the two callbacks are doing, since the names sound like they are doing the same thing. The reason I ask is that I have to use a third-party toolbox that overwrites the contents of InitFcn everytime the model is re-started, but the interface with the app appears to work correctly if I move the definition of hApp into StartFcn.
In other words, why do you put the definition of hApp in InitFcn, and what's the difference between InitFcn and StartFcn?

Jesica N
Jesica N on 26 Jul 2022
Hi , I have question regarding event listener.
How to proceed with this method if i have to display multiple displaybox to app designer?

Jack Daniels
Jack Daniels on 16 Nov 2022 at 11:27
I'd like to access DATA from Simulink model which are Matrix format and disply them
How can I access the "MATRIX" and display in 3D surf graph?
Thank you for your help!


Find more on Event Functions in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!