How do I exit a loop with a key hit?

So I have a loop in my code but I want to exit the loop and move onto the next set of calculations after I hit some key. I know that I could ask for input in the loop, which could confirm whether I could move on or not but that would require me to confirm at each loop which I don't want to be doing.

Respuestas (4)

Lukas
Lukas el 22 de En. de 2020
In a similar way I use wait bat cancel button:
hWaitbar = waitbar(0, 'Iteration 1', 'Name', 'Solving problem','CreateCancelBtn','delete(gcbf)');
for i=1:5
% Some long taking computation
pause(5);
% Check
drawnow;
if ~ishandle(hWaitbar)
% Stop the if cancel button was pressed
disp('Stopped by user');
break;
else
% Update the wait bar
waitbar(i/5,hWaitbar, ['Iteration ' num2str(i)]);
end
end

2 comentarios

Sanders A.
Sanders A. el 5 de Feb. de 2020
I prefer this answer because it doesn't latch on to an existing figure and upon hitting the cancel/'stop now' button delete the figure. This si nice for being able to leave my figures in specific places on my screen and run&stop my animations as I please.
Thank you!!
Lorenzo
Lorenzo el 14 de Nov. de 2024
excellent! Thank you

Iniciar sesión para comentar.

Vugar
Vugar el 30 de Sept. de 2018

1 voto

The answer of Jan is almost correct - only one code line is missing:

ButtonHandle = uicontrol('Style', 'PushButton', ...
                         'String', 'Stop loop', ...
                         'Callback', 'delete(gcbf)');
for k = 1:1e6
  disp(k)
  if ~ishandle(ButtonHandle)
    disp('Loop stopped by user');
    break;
  end
pause(0.01); % A NEW LINE
end
Jan
Jan el 1 de Jul. de 2013
Editada: Jan el 1 de Jul. de 2013
You can create a waitbar or any other GUI which contains a button for breaking the loop. Then check a property inside the loop and break is the value chnages. This property could be the existence of the figure or the button as well as the UserData of the button or figure.
ButtonHandle = uicontrol('Style', 'PushButton', ...
'String', 'Stop loop', ...
'Callback', 'delete(gcbf)');
for k = 1:1e6
disp(k)
if ~ishandle(ButtonHandle)
disp('Loop stopped by user');
break;
end
end

8 comentarios

Tom
Tom el 25 de Jul. de 2016
I tried your code but when i run the script, the pushbotton never appears. The code runs and displays the k values, but the opportunity to press the pushbutton and then enter the if statement never occurs.
Only when i press cntrl + c does the pushbutton appear.
Eyman FAKHRI
Eyman FAKHRI el 25 de Abr. de 2018
Editada: Eyman FAKHRI el 25 de Abr. de 2018
Hello, have you find the solution for this problem ?
Mickael Pruvost
Mickael Pruvost el 25 de Jun. de 2019
Editada: Mickael Pruvost el 25 de Jun. de 2019
Hi, is anyone found why this code doesn't show the figure and the button to stop the loop?
Phillip Warren
Phillip Warren el 26 de Jul. de 2019
I've discovered (in Matlab R2018b) that placing a pause(1) before the for loop allows for the figure to pop up. However, the button does not stop the loop.
Rik
Rik el 26 de Jul. de 2019
You may need to call drawnow to force queued callbacks to be executed. The same would be true before the loop. The pause function implicitly calls drawnow.
S. Gokhun Tanyer
S. Gokhun Tanyer el 5 de Ag. de 2019
Did not work for version: 9.5.0.944444 (R2018b) :(
Rik
Rik el 5 de Ag. de 2019
S. Gokhun Tanyer, what is the exact code you tried?
Christopher Bitikofer
Christopher Bitikofer el 23 de Dic. de 2022
clarifying point. the uicontrol function tries to draw on the current figure, When I didn't have one open it didn't instantiate its own figure. I setup the following code to initialize the uibutton on an independant figure.
breakLoopFigure = figure('color','w','Name','Plotter');
breakLoopFigure.Position = [1325 612 560 420];
breakLoopFigure.Visible = "on";
breakLoopFigure.Units = "normalized";
ButtonHandle = uicontrol(breakLoopFigure,...
'Style','pushbutton',...
'String', 'Stop loop', ...
'Callback', 'delete(gcbf)');
ButtonHandle.Units = "normalized";
ButtonHandle.Position = [.1 .1 .8 .8];
drawnow
This works quite well for my purpose, but you could just as easily place the button on an app gui or a plot with other data. Others have pointed out the waitbar also works well.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 1 de Jul. de 2013

Comentada:

el 14 de Nov. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by