How to stop a while loop using a GUI pushbutton?

조회 수: 107 (최근 30일)
Flávio
Flávio 2013년 11월 13일
댓글: aung lin 2022년 7월 12일
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?
Thanks!

답변 (6개)

Walter Roberson
Walter Roberson 2013년 11월 13일
while true
drawnow()
stop_state = get(YourPushbuttonHandle, 'Value');
if stop_state
break;
end
...
end
  댓글 수: 7
Image Analyst
Image Analyst 2018년 6월 29일
App Designer has a different way of using global variables I believe - you don't use the global keyword.
Stephen23
Stephen23 2018년 6월 29일
@Image Analyst: I have never used App Designer, but as far as I can tell it creates some kind of objects, whose properties are available throughout the app:
What does this have to do with global variables?

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2018년 6월 29일
See my attached little demo. It counts when you click the Go button and stops counting when you click the Stop button.
  댓글 수: 5
Haotian Xue
Haotian Xue 2021년 3월 16일
It works great, thank you so much.
aung lin
aung lin 2022년 7월 12일
Thank You very much Sir!

댓글을 달려면 로그인하십시오.


Birkan GOCERLER
Birkan GOCERLER 2014년 9월 21일

Andrew Davies
Andrew Davies 2015년 9월 15일
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
while ~(handles.stop_now)
yourfunction()
drawnow %Give the button callback a chance to interrupt the %opening fucntion
handles = guidata(hObject); %Get the newest GUI data
end
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
Greig 2015년 9월 16일
편집: Greig 2015년 9월 16일
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
Seo-Hyun 2017년 4월 12일
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.
while(1)
if handles.pause.Value
pause(1);
handles.pause.String = 'Resume';
disp('waiting');
while ~handles.pause.Value
handles.pause.String = 'Pause';
break;
end
continue;
end
end
function pause_Callback(hObject, eventdata, handles)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by