How can I get MATLAB to leave a loop when any key is pressed?
조회 수: 5 (최근 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에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!