MATLAB Answers

Raldi
0

Stop a while loop using a GUI

Asked by Raldi
on 24 Jan 2013
Latest activity Answered by Birkan GOCERLER on 21 Sep 2014
Accepted Answer by Jan
Hey everyone,
I need to stop a while loop using a 'Stop' button in a GUI and i was wondering what are the best ways of doing that.

  0 Comments

Sign in to comment.

3 Answers

Jan
Answer by Jan
on 24 Jan 2013
Edited by Jan
on 24 Jan 2013
 Accepted Answer

A short example:
h = uicontrol('Style', 'PuchButton', 'String', 'Stop', ...
'Callback', 'delete(gcbo)');
while ishandle(h)
disp(datestr(now, 0));
pause(0.25);
end
Check out the famous 41 GUI example.

  2 Comments

Raldi
on 24 Jan 2013
Ill go with this answer as i already have created a pushbutton. Will there be a problem if the pushbutton is created in another function and i just use findobj or something similar?
Jan
on 24 Jan 2013
On one hand: no, you can create the button anywhere and use any method to obtain the handle. On the other hand: simply try it - when you do not have problems, it works.

Sign in to comment.


Answer by Sean de Wolski
on 24 Jan 2013

Have the while-loop check if a togglebutton uicontrol has been pushed (i.e. what is it's value). If it has the loop will end
while(get(handles.togglebutton,'value')); %handles.togglebutton is handle to button
do_stuff;
drawnow; %force event queue update
end

  0 Comments

Sign in to comment.