How can i get one GUI to trigger another GUI?

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.

 Réponse acceptée

Niels
Niels le 13 Fév 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.
GUI_1
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)
end
function whileLoop(hObj, ~)
hs = guidata(hObj);
set(hs.indicatorBox,'String','While-loop active!')
set(hs.startLoop, 'String', 'Running!')
while true
drawnow
if ~hObj.Value
set(hs.indicatorBox,'String','Stopped. Press the button...')
set(hs.startLoop, 'String', 'Stopped!')
break;
end
set(hs.randomValue, 'String', num2str(rand,'%.2f'))
hs = guidata(hObj);
pause(0.2)
end
end
GUI_2
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)
end
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)
end
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.

4 commentaires

Stig Eriksen
Stig Eriksen le 13 Fév 2017
Hi Niels. Thank you very much for the answer, I can definitely see how the listener would be the way to go. Being a MATLAB novice however I cannot figure out how to implement it in my GUIDE generated GUI. Where should I place the listener? Should it be just after the part where GUI2 is made visible? My code is quite long (>1000 lines by now) so i dont think its a good idea to paste it all here. I could create and paste a shorter code that looks like it if it would help
Niels
Niels le 14 Fév 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 le 15 Fév 2017
After a good deal of messing around I finally got the listener working, thanks for the help
Niels
Niels le 16 Fév 2017
Good to hear. How did you implement it in the end? :)

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 13 Fév 2017

0 votes

Instead of a while loop, a timer can trigger the updates more reliably in both GUIs.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by