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

105 views (last 30 days)
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?

Answers (4)

aumit
aumit on 20 Sep 2017
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 on 13 May 2019
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 on 13 Sep 2019
Edited: Ankit on 11 Dec 2019
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 Comments
wenchang li
wenchang li on 20 Apr 2021
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 on 7 Jun 2023
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 ?

Sign in to comment.


Chris Portal
Chris Portal on 5 Nov 2016
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 Comments
Chris Portal
Chris Portal on 15 Nov 2016
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 on 31 Jul 2017
Edited: Jon on 31 Jul 2017
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?

Sign in to comment.

Categories

Find more on Get Started with Simulink 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!