Break Out of While Loop with ESC key

13 visualizaciones (últimos 30 días)
Amjad
Amjad el 18 de Jun. de 2012
Hi I want something like below.
while(see if esc key is pressed)
reading serial port;
writing to another serial port;
end
code;
code;
fclose(serialports);
where the while loop continues to execute until the escape key is pressed. Currently i am doing it with while(1). but when i have to end the program i have to press ctrl+c. Which terminates the program and the code where the ports are to be closed is not executed. So i want to get out of the while loop when escape is pressed and the code below the while loop is also executed.

Respuesta aceptada

Jan
Jan el 18 de Jun. de 2012
What about a small GUI?
DlgH = figure;
H = uicontrol('Style', 'PushButton', ...
'String', 'Break', ...
'Callback', 'delete(gcbf)');
while (ishandle(H))
disp(clock);
pause(0.5);
end
  1 comentario
Amjad
Amjad el 19 de Jun. de 2012
Thanks for the answer. It does meet the requirement.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 18 de Jun. de 2012
With respect to cleanup on control-c: see onCleanup()
  1 comentario
Amjad
Amjad el 19 de Jun. de 2012
Yes it will clean up the variables at least. but i think the stop process button would be more feasible then ctrl+c.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by