MATLAB Answers

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

224 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2019 年 2 月 15 日
回答済み: Serhiy Kapustyan 2021 年 2 月 23 日 10:00
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?

採用された回答

MathWorks Support Team
MathWorks Support Team 2020 年 8 月 10 日
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;
end

  3 件のコメント

Perry
Perry 2020 年 3 月 25 日
I want to run model using 'Simulink Desktop Real-Time', but the callback function does not run. 'Edit File' in GUI doesn't change. (work fine in normal mode)
I try to replace
listener = @(app, event) updateGUI(hApp);
with
listener = 'disp(''Running'')';
Nothing displays in 'Diagnostic Viewer' , so event listener does not run at all.
I have no ideal why event listener doesn't run. Do you have any solutions?
Thanks.
Alireza Ghaderi
Alireza Ghaderi 2020 年 8 月 23 日
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...

サインインしてコメントする。

その他の回答 (1 件)

Serhiy Kapustyan
Serhiy Kapustyan 2021 年 2 月 23 日 10:00
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?

  0 件のコメント

サインインしてコメントする。

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by