MATLAB Answers

How can I interrupt a for or while loop by pressing a button 2?

3 views (last 30 days)
Hi everyone! I have a GUI that populates an excel sheet through a for loop. This for loop I activate through a button 1. However, I am trying to add a button 2 that stops the for loop and that shows a text in the command window through disp () but this button 2 does not work until I lock the loop with Ctrl + C in the command window. Once the loop has been stopped manually and not through the button, the queued messages are seen in the command window. QUESTION: Can someone help me to interrupt the loop through button 2? Thank you very much in advance!
properties (Access = public)
num_anterior
txt_anterior
raw_anterior
num_anterior2
txt_anterior2
raw_anterior2
datos2
nombre2
bloqueo = 1;
end
for l=1:height(ficheroAnterior)
bloqueo = app.bloqueo;
if bloqueo == 1
PrPres = [];
[rowPrPres, colPrPres] = find(string(raw_actual(:,precioArellenar))==string(ficheroAnterior(l,1)));
PrPres = str2num(ficheroAnterior(l,2));
for r=1:length(rowPrPres)
Rango = strcat(columnaExcelaRellenar,string(rowPrPres(r)));
writematrix(PrPres,app.datos2,'Range',Rango);
l
end
else
break
end
end
function PararejecucionButtonPushed(app, event)
app.bloqueo = 0;
disp("paro ejecucion")
end

Accepted Answer

Rik
Rik on 6 Oct 2021
You can tell Matlab to process callbacks with the drawnow function.
If you put a call to drawnow just before you reload the value of your flag, the callback to the stop button will be processed and the value of the flag set to 0.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!