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

조회 수: 15 (최근 30일)
Cristian Tacoronte
Cristian Tacoronte 2021년 10월 6일
댓글: Cristian Tacoronte 2021년 10월 6일
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

채택된 답변

Rik
Rik 2021년 10월 6일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by