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