How can i get one GUI to trigger another GUI?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Niels
2017-2-13
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 个评论
更多回答(1 个)
Jan
2017-2-13
Instead of a while loop, a timer can trigger the updates more reliably in both GUIs.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!