How to stop a while loop using a GUI pushbutton?
142 visualizzazioni (ultimi 30 giorni)
I have a while loop and I have a STOP pushbutton that breaks the loop. Basically I want to break the loop using the pushbutton instead of typing CTRL+C on command window. I tried the return clause but it didn't work and the break clause aparently it's used inside the loop (which isn't the case).
Can anyone help me?
Walter Roberson il 13 Nov 2013
stop_state = get(YourPushbuttonHandle, 'Value');
Birkan GOCERLER il 21 Set 2014
Andrew Davies il 15 Set 2015
The solution to the problem can be found here http://stackoverflow.com/questions/4522447/breaking-from-for-loop-in-matlab-gui
the basic outline could look like this:
For the callback of the start push button you want something like this.
functionpushbutton1_Callback(hObject, eventdata, handles)
handles.stop_now = 0; %Create stop_now in the handles structure
guidata(hObject,handles); %Update the GUI data
drawnow %Give the button callback a chance to interrupt the %opening fucntion
handles = guidata(hObject); %Get the newest GUI data
In the callback for the stop button you want
function pushbutton2_Callback(hObject, eventdata, handles)
handles.stop_now = 1;
guidata(hObject, handles); % Update handles structure
Greig il 16 Set 2015
Modificato: Greig il 16 Set 2015
Essentially of the these solutions are based on the same ideas. Another approach (that uses the same idea) is to use the MATLAB inbuilt waitbar() function, which supports canceling a loop and details how to do in the documentation... FOUND HERE. This way you don't have to explicitly program the canceling functionality of your button 2, which is useful if you are still getting to grips with GUIs.
Edit: And only after answering do I realize this is a somewhat old thread!
Seo-Hyun il 12 Apr 2017
I used a toggle button for pause, and added the pause code in the loop. While pausing, you can always grab a breakpoint in the paused line and continue debug. Try this.
handles.pause.String = 'Resume';
handles.pause.String = 'Pause';
function pause_Callback(hObject, eventdata, handles)