How to stop a while loop using a GUI pushbutton?

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 日

5 投票

while true
drawnow()
stop_state = get(YourPushbuttonHandle, 'Value');
if stop_state
break;
end
...
end

7 件のコメント

Flávio
Flávio 2013 年 11 月 13 日
It doesnt seem to work. What do i write in the pushbutton callbackfunction? The thing is, i have a pushbutton 1 that runs my code with the while loop. Then i have a pushbutton 2 that stops the loop. What do i put in button2 callback? I tried your code but nothing happens when i hit button2.
Thanks!
ZacksHacks
ZacksHacks 2014 年 6 月 12 日
編集済み: ZacksHacks 2014 年 6 月 12 日
Some people might find it crude, but you can just use a global variable if you are not familiar with GUI syntax and just want a quick fix.
%Button 1 Click
global true;
true = 1;
while true == 1
drawnow()
%Your loop code here
end
%Stop Button Click
global true;
true = 0;
Vibhor Gulati
Vibhor Gulati 2016 年 5 月 26 日
Oh you saviour! Nothing else seemed to work for me.
Jannis
Jannis 2018 年 6 月 29 日
Super helpful, also works in appdesign.
Stephen23
Stephen23 2018 年 6 月 29 日
It is recommended to avoid slow and buggy global variables:
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 日

4 投票

See my attached little demo. It counts when you click the Go button and stops counting when you click the Stop button.

5 件のコメント

Liza Vörös
Liza Vörös 2020 年 3 月 23 日
It does not work
Liza Vörös
Liza Vörös 2020 年 3 月 23 日
I get this error:
Dot indexing is not supported for variables of this type.
Error in gostop>btnStop_Callback (line 103)
set(handles.btnStop,'userdata',1)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in gostop (line 45)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)gostop('btnStop_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Dot indexing is not supported for variables of this type.
Error in gostop>btnGo_Callback (line 84)
set(handles.btnStop, 'userdata', 0);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in gostop (line 45)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)gostop('btnGo_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Image Analyst
Image Analyst 2020 年 3 月 23 日
I just downloaded it again and it works fine. When it says "Dot indexing is not supported" for the handles variable, it indicates to me that somehow your handles structure got corrupted. Did you modify my file at all? Like put a clear statement in there somewhere? Please attach your exact files and I'll download and run your version.
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!

サインインしてコメントする。

Andrew Davies
Andrew Davies 2015 年 9 月 15 日

2 投票

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 日

0 投票

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 日

0 投票

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)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2013 年 11 月 13 日

コメント済み:

2022 年 7 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by