App Designer to control a Simulink Model and read back model data

65 ビュー (過去 30 日間)
Justin Mellor
Justin Mellor 2016 年 11 月 4 日
I am trying to create a GUI using App Designer 2016b that will control inputs to the model AND read back model values while the Simulink model is running. I can control the model from an app by including set_param commands in the app callback functions. As per an earlier GUIDE example I have included a callback with the Simulink model that sets up an event listener and calls a Matlab function to update the GUI with the output value of a Gain block. However I do not know how to update the properties of a Gauge in the GUI with the value from the Simulink model. How do I do this? Could someone provide an example please?

回答 (4 件)

aumit
aumit 2017 年 9 月 20 日
Hi,
I am trying to use this to output the status of my Simulink simulation in an Edit Field in the GUI. Your suggestions do not work for me.
My code in PreLoadFcn is
hApp = Setup;
and in a Matlab function called from Simulink
function guicomm()
happ.StatusEditField.Value = 'Test';
where Setup is the name of my app and StatusEditField the name of the Edit Field. I can see happ in the workspace and if I click on the respective entries I can change the value manually succesfully. If I then try running
happ.StatusEditField.Value = 'Test';
in the Command window, a new structure is created and the original field remains unchanged. If I skip Simulink and enter everything in the Command Window, it works. Do you know what could have gone wrong? Could you please provide a more detailed example? Thanks in advance.

Rahul Murmude
Rahul Murmude 2019 年 5 月 13 日
Can you share the details on how to control the model from an app? I also need to achieve a similar objective. Where I can have a drop down list for giving input parameters to simulink model and then run the model through the app.

Ankit
Ankit 2019 年 9 月 13 日
編集済み: Ankit 2019 年 12 月 11 日
Update 21.11.2019:
Please check my submission in the below link:
https://de.mathworks.com/matlabcentral/fileexchange/73613-interface-between-matlab-app-and-simulink
------------------------------------------------------------------------------------------------------------------
In a similar way to GUIDE you can read real-time values from Simulink Model.
As a initial step it is required to defined the Tag in App designer opening Function: (Why we need to define Tag manually in App Designer?)
app.test.Tag = 'test';
Then work to be done on Simulink Model.
1st Step: Model Properties -> Callbacks ->
% Set up the arguments that will go into the gain block event callback listener
blk = 'modelname/outputportname';
event = 'PostOutputs';
listener = @updateApp; (separate MATLAB function)
% Create the listener
h = add_exec_event_listener(blk, event, listener);
2nd Step: MATLAB Function (updateApp.m)
%create a run time object
rto = get_param('modelname/outputportname','RuntimeObject');
str = num2str(rto.InputPort(1).Data);
% get a handle to the Edit Button in MATLAB App
all_tag_objects = findall(0, '-property', 'tag');
all_tags = get(all_tag_objects, 'tag');
[tf, idx] = ismember('test', all_tags);
if tf
st1 = all_tag_objects(idx);
end
% update the MATLAB App
set(st1,'Value',str2double(str));
  2 件のコメント
wenchang li
wenchang li 2021 年 4 月 20 日
Thank you so much for sharing. But I got a very slow simulation after adding the listener. How can I slove it?
el warraki abdelhadi
el warraki abdelhadi 2023 年 6 月 7 日
for example if i want to test 'outputportname' if it equal to a number in app designer while simulation is on what can i do ?

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


Chris Portal
Chris Portal 2016 年 11 月 5 日
Assuming you have your app running when you want the model to update the Gauge, you could achieve this a couple of ways. In both cases, you'll need a handle to the running app, which is returned as a left hand side argument when you first launch the app, i.e.
hApp = mysimulinkapp;
1) Update the gauge by accessing it directly in your Simulink callback. Assuming the name you gave the gauge in App Designer's Component Browser was "GainGauge", you'd use the syntax:
hApp.GainGauge.Value
2) Alternatively, create a public function in your app that takes care of updating the gauge or any other updating logic you need, and call that function from your Simulink callback. Assuming the public function you create is named "myAppUpdateRoutine", the syntax you'd use to call it is:
myAppUpdateRoutine(hApp, ...)
Hope this helps!
  3 件のコメント
Chris Portal
Chris Portal 2016 年 11 月 15 日
The steps I described would be the way to do it. Did it not work for you, or are the steps unclear? I'm going by your statement of:
"...I have included a callback with the Simulink model that sets up an event listener and calls a Matlab function to update the GUI..."
In the MATLAB function your listener calls, that is where you would either call:
hApp.GainGauge.Value
or
myAppUpdateRoutine(hApp, ...)
Jon
Jon 2017 年 7 月 31 日
編集済み: Jon 2017 年 7 月 31 日
In the first case, where the MATLAB function calls, hApp.GainGauge.Value, if you launch the app in the models startFcn, how do you pass the handle (hApp) to the listener?

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

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by