MATLAB Answers

0

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

Asked by Justin Mellor on 4 Nov 2016
Latest activity Edited by Ankit
on 17 Sep 2019
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?

  0 Comments

Sign in to comment.

4 Answers

Answer by 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

Thanks for getting back to me on this. I obviously need to launch the app just once, but update the gauge value at each sample time. Is there an easy way to do this? Justin
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, ...)
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.


Answer by 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.

  0 Comments

Sign in to comment.


Answer by 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.

  0 Comments

Sign in to comment.


Answer by Ankit
on 13 Sep 2019
Edited by Ankit
on 17 Sep 2019

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));

  0 Comments

Sign in to comment.