MATLAB Answers

How to prevent App Designer GUI from opening multiple GUI figures?

24 views (last 30 days)
Justin Coe
Justin Coe on 5 Feb 2019
Answered: galaxy on 8 Jun 2021
I am trying to create a GUI in App Designer that will display values from a Simulink model. Using the add_exec_event_listener I am able to get the GUI to change the value of a numeric EditField based on the model. The problem is that every time the value changes and the GUI should update, it just opens up a new figure that has the new value. It doesn't just change the existing figure to match the new value, which is what I want it to do. I have looked around quite a bit but cannot seem to find anyone else that has posted this problem or anything similar.
My current simulink model is just a constant going to a display box (it will be far more complicated, but this is just for me to sovle this problem). My Simulink model callbacks are:
blk = 'Trial1/DisplayBox'; % Trial1 is the name of the Simulink model and DisplayBox is what I have named the Display
event = 'PostOutputs';
listener = @(app,event) updateGUI(TrialApp1);
h = add_exec_event_listener(blk,event,listener);
My updateGUI function in App Designer is written as:
methods (Access = public)
function updateGUI(app,varargin)
rto = get_param([bdroot,'/DisplayBox'],'RuntimeObject');
app.EditField.Value = rto.InputPort(1).Data
I am guessing that the issue lies with how I have my model callbacks written in the Simulink model, but I am really not sure. This format seems to match multiple other places and asnwers that I have found but it seems I have made a mistake somewhere.

Answers (3)

Justin Coe
Justin Coe on 21 Feb 2019
Edited: Justin Coe on 1 Mar 2019
MathWorks contacted me and was able to easily fix my problem, so I will post the solution here for anyone with a similar issiue.
My InitFcn should be written as:
hApp = TrialApp1
Which would then change the listener in my StartFcn to be:
listener = @(app,event) updateGUI(hApp);
That was the only problem. Ther reason is because if you don't define a handle for the app (TrialApp1) then it ends up constructing the app each time. This is in the automatic code created in the grayed section beneath where the user edits are written.
Hopefully my oversight can help someone else in the future if you make the same mistake!
  1 Comment
joao lopes
joao lopes on 12 Nov 2019
Hi, I tried to implement the changes that you said but I can't prevent the simulink from openning multiple GUI's. can you help me?
Should I post the "hApp = TrialApp1" code in InitFcn of the all model or just in the displaybox(in you'r case)?

Sign in to comment.

galaxy on 8 Jun 2021
I tried in Matlab 2019b, it still have samed problem.
I don't know how to fix.
Do you have idea for me??




Community Treasure Hunt

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

Start Hunting!

Translated by