Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a infinite while loop pressing a key in Matlab

I have a while loop, infinite, and I want to stop it when I press a keyboard key.

Pseudocode:

While(1)
    do stuff;

    listening for key;
    if key is pressed
        break;
    end
end

The function waitforbuttonpress makes me press the key, so no luck.

I've found no option on the web.

like image 748
SamuelNLP Avatar asked Dec 17 '25 11:12

SamuelNLP


1 Answers

OK, I know this is a bit late but, I found a solution after a long hunt. When a figure window is focused, you can do this:

set(gcf,'currentchar',' ')         % set a dummy character
while get(gcf,'currentchar')==' '  % which gets changed when key is pressed
   do_stuff()
end

When a key is pressed while focus is in a figure, it sets the property 'currentchar'. Tested in R2010b.

like image 110
Sanjay Manohar Avatar answered Dec 19 '25 07:12

Sanjay Manohar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!