How to break a while loop in a gui toggle button callback?

6 visualizzazioni (ultimi 30 giorni)
I have created a GUI with a pushbutton set to toggle mode, and within the callback for the buttton, I have a while loop running. The while loop begins after I press the GO button ("pushbuttonGO" in the code), and I am trying to get it to break by sensing the false value of a boolean variable in the handles structure (handles.RUNNING -> false).
This method is not working. When I press the button to stop, the while loop continues.
Can anyone tell me either how to correctly sense (e.g. by correct syntax for getting updated handles data) the boolean handles.RUNNING within this loop, or where to move the while loop so that operation stops when I press the toggle button again.
The code for the pushbutton callback is below. I am not sure how to attach both the .m and .fig files here.
Thank you.
% --- Executes on button press in pushbuttonGO.
function pushbuttonGO_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonGO (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject, 'Value')
handles.RUNNING = true; %toggle bool on
guidata(hObject,handles); %update handles
set(handles.pushbuttonGO,'BackgroundColor',[0.9,0,0]);
set(handles.pushbuttonGO,'String','Running');
set(handles.editTextNotice,'String','pushed GO');
pause(0.3);
timeInterval = 0.5; %s
set(handles.editTextInterval,'String',sprintf('%.2f',timeInterval));
while 1
X = rand(5000,30*4);
Xstr = sprintf('%.1f,',X(1:10,:));
set(handles.editTextData,'String',Xstr);
pause(timeInterval);
handles=guidata(hObject); %PROBLEM?
if ~handles.RUNNING %! Never breaks
break
end
end
else
set(handles.pushbuttonGO,'BackgroundColor',[0,0.9,0]);
set(handles.editTextNotice,'String','OFF');
set(handles.pushbuttonGO,'String','Press to Run');
guidata(hObject, handles); %update handles
handles.RUNNING = false; %toggle bool off
end

Risposta accettata

Matt Fig
Matt Fig il 16 Nov 2012
Here is an example. Basically you have to flush the event queue by calling DRAWNOW or PAUSE to check if the state of the button has changed. Try this out.
function [] = gui_toggle()
% How to stop a while loop with a toggle button.
S.f = figure('name','togglegui',...
'menubar','none',...
'numbert','off',...
'pos',[100 100 300 150]);
S.t = uicontrol('Style','toggle',...
'Units','pix',...
'Position',[10 10 280 130],...
'CallBack',@callb,...
'String','No Loop');
movegui('center')
function [] = callb(varargin)
set(S.t,'string','Looping!')
drawnow
while 1
sort(rand(1010101,1)); % Put your code here.
drawnow
if ~get(S.t,'value')
set(S.t,'string','No Loop')
break
end
end
end
end
  2 Commenti
George
George il 16 Nov 2012
Modificato: George il 16 Nov 2012
Dear Matt - Thank you very much for your solution. Your code does work and provides excellent example for a simplified gui, and I could restructure my code in that manner. However, the ‘drawnow’ solution does not work for my code. Adding drawnow in the while loop of my code has no effect (I already had a "pause" that should have the same effect as drawnow). Your solution did have the key to making my code work correctly. That is, if I change the condition for breaking the while loop from “if ~handles.RUNNING” to “if ~get(hObject, ‘value’)”, that gets the value of the pushbutton, then my code works as desired. However, that means that the while loop is responding only to the value of this uicontrol; and still not to another variable. This gets me back to the question of how to make the loop respond to the value of a variable, such as “handles.RUNNING” that changes during while loop execution elsewhere. Basically, how do I correctly manipulate the fields of the handles struct so that they are detected during execution, because apparently there are nuances of the “guidata” command and/or the guidata command has limitations that I have not understood. Related, it seems that the “hObject” that comes into a gui function is not necessarily the correct object handle for the guidata command, for example, after entering a function; why?
Here’s my working code.
% --- Executes on button press in pushbuttonGO.
function pushbuttonGO_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonGO (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(hObject, 'Value')
set(handles.pushbuttonGO,'BackgroundColor',[0.9,0,0]);
set(handles.pushbuttonGO,'String','Running');
set(handles.editTextNotice,'String','pushed GO');
pause(0.3);
timeInterval = 0.5; %s
set(handles.editTextInterval,'String',sprintf('%.2f',timeInterval));
while 1
X = rand(5000,100*4);
Xstr = sprintf('%.1f,',X(1:10,:));
set(handles.editTextData,'String',Xstr);
pause(timeInterval);
if ~get(hObject, 'Value')
break
end
end
else
set(handles.pushbuttonGO,'BackgroundColor',[0,0.9,0]);
set(handles.editTextNotice,'String','OFF');
set(handles.pushbuttonGO,'String','Press to Run');
end
Matt Fig
Matt Fig il 16 Nov 2012
It appears you are correct. The application data doe not update during a while loop. I did try to set and unset a value in the roots userdata property and that worked.
set(0,'userdata',1)
So give this a try. In your function when the condition is met, simply switch the value of the root's userdata. Check this in the while loop.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by