How to break out of a while loop inside a function block ?
Show older comments
I'm trying to break out of a while loop when a key is pressed and I've achieved this through the following code snippet -
keep = 1;
h = figure('KeyPressFcn','keep=0');
while keep
pause(0.01)
end
close(h)
I need to incorporate this into a GUI. But when this exact same code snippet is inserted into a function block, I'm no longer able to break out of the loop. Here's a code for the same -
function calibrate()
keep = 1;
h = figure('KeyPressFcn','keep=0');
while keep
pause(0.01)
end
close(h)
How do I get this to work ?
Thanks !
1 Comment
Muthu Annamalai
on 28 Jul 2015
Please use {} Code editor feature to make your code more readable to others.
Accepted Answer
More Answers (1)
Muthu Annamalai
on 28 Jul 2015
Edited: Muthu Annamalai
on 28 Jul 2015
Changing your code to,
keep = true;
h = figure('KeyPressFcn',@() keep= ~keep );
while keep
pause(0.01)
sprintf('keep = %d',keep);
end
close(h)
may work. If this doesn't work, make keep an instance of a handle class.
1 Comment
Prashanth Krishnan
on 28 Jul 2015
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!