How can I get MATLAB to leave a loop when any key is pressed?
23 次查看(过去 30 天)
显示 更早的评论
I have a MATLAB script that contains a(n endless) loop. I want the loop to be exited when a key is pressed?
0 个评论
回答(1 个)
José-Luis
2012-12-21
编辑:José-Luis
2012-12-21
This is a bit of a hack, and will probably slow down your code. The idea is to create a figure, and check whether a key has been pressed when the figure is active. Ergo, you need to click on the figure window and press a key while the cursor is over it.
WARNING: The following is an infinite loop. Use ctrl+c to break it if problems arise.
h = figure(1);
forever = 1;
while forever
drawnow
isKeyPressed = ~isempty(get(h,'CurrentCharacter'));
if isKeyPressed
break
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!