MATLAB Answers

How can i get one GUI to trigger another GUI?

4 views (last 30 days)
Stig Eriksen
Stig Eriksen on 13 Feb 2017
Commented: Niels on 16 Feb 2017
I have two GUI's running together simulating the running of a ships engine room. The main GUI (GUI_1) runs a while loop, started with a button press, calculating stuff like fuel consumption, propeller thrust and ship speed. The while loop runs continuously while the button is pressed but has a 0.2 sec pause build in. In the second GUI (GUI_2) I would like to display additional data as well as plots of selected parameters calculated in the main GUI. I have managed to pass data between the GUI's with the setappdata getappdata method. Getting continuously updating data from GUI_2 to GUI_1 works fine because the getappdata is included in the while loop in GUI_1. I can get data from GUI_1 to GUI_2 but so far it only updates on activation of a button in GUI_2, not continuously. My question is how do I trigger GUI_2 to receive and display data from GUI_1 continuously (or at the same 0.2 sec iteration as GUI_1)?
I tried putting a while loop in GUI_2 but that made the program stall (I am guessing GUI_1 just waited for the GUI_2 loop to finish, which it did not do since there was no end to the loop)
The GUI's are created in GUIDE, no toolboxes are in use and I am running the r2015b academic version of MATLAB. Thank you for your help.


Sign in to comment.

Accepted Answer

Niels on 13 Feb 2017
What you could do is add a listener in GUI_2 to an object in GUI_1 that is updated when your while-loop is being executed. You could use the callback function to define anything that needs to be updated.
See below dummy code.
function GUI_1
hs.hFig = figure('Menubar','none', 'Visible','On', 'Name','GUI_1', ...
'Units','Pixels', 'Position',[100 100 300 300]);
hs.startLoop = uicontrol('Style','togglebutton', 'Parent',hs.hFig, ...
'String','Start!', 'Tag','StartLoop', ...
'Units','Normalized', 'Position',[0.1, 0.5, 0.8, 0.4], 'Callback',@whileLoop);
hs.indicatorBox = uicontrol('Style','text', 'Parent',hs.hFig, ...
'String','Press the button...', 'Tag','iBox', ...
'Units','Normalized', 'Position',[0.1, 0.2, 0.8, 0.1]);
hs.randomValue = uicontrol('Style','text', 'Parent',hs.hFig, ...
'String','0', 'Tag','rValue', ...
'Units','Normalized', 'Position',[0.1, 0.05, 0.8, 0.1]);
guidata(hs.hFig, hs)
function whileLoop(hObj, ~)
hs = guidata(hObj);
set(hs.indicatorBox,'String','While-loop active!')
set(hs.startLoop, 'String', 'Running!')
while true
if ~hObj.Value
set(hs.indicatorBox,'String','Stopped. Press the button...')
set(hs.startLoop, 'String', 'Stopped!')
set(hs.randomValue, 'String', num2str(rand,'%.2f'))
hs = guidata(hObj);
function GUI_2
hs.hFig = figure('Menubar','none', 'Visible','On', 'Name','GUI_2', ...
'Units','Pixels', 'Position',[400 100 300 300]);
hs.randomValue = uicontrol('Style','text', 'Parent',hs.hFig, ...
'String','0', 'Tag','rValue');
theOtherFigure = findobj('Type','figure','Name','GUI_1');
hs2 = guidata(theOtherFigure);
hs.myListener = addlistener(hs2.randomValue, 'String', 'PostSet', @UpdateInThisGUI);
guidata(hs.hFig, hs)
function UpdateInThisGUI(varargin)
hs = guidata(findobj('Type','figure','Name','GUI_2'));
hs2 = guidata(findobj('Type','figure','Name','GUI_1'));
set(hs.randomValue,'String', hs2.randomValue.String)
A nicer way might be to create a class for your data with a custom event that is triggered whenever your data is updated. For that, you'd have to look into MATLAB's OOP functionality.


Show 1 older comment
Niels on 14 Feb 2017
I have never used GUIDE (just cannot get used to its output), but I think it should be possible to do this in your OpeningFcn for GUI_2.
The alternative would be to add some kind of "start sync"-button in GUI_2 where you could create the listener in its corresponding callback function.
Stig Eriksen
Stig Eriksen on 15 Feb 2017
After a good deal of messing around I finally got the listener working, thanks for the help
Niels on 16 Feb 2017
Good to hear. How did you implement it in the end? :)

Sign in to comment.

More Answers (1)

Jan on 13 Feb 2017
Instead of a while loop, a timer can trigger the updates more reliably in both GUIs.


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by