How can I get MATLAB to leave a loop when any key is pressed?
17 ビュー (過去 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!