循环按下Matlab中的一个键可停止无限循环 [英] Stop a infinite while loop pressing a key in Matlab
本文介绍了循环按下Matlab中的一个键可停止无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个while循环,它是无限的,我想在按下键盘键时将其停止.
I have a while loop, infinite, and I want to stop it when I press a keyboard key.
伪代码:
While(1)
do stuff;
listening for key;
if key is pressed
break;
end
end
功能waitforbuttonpress
使我按下该键,所以没有运气.
The function waitforbuttonpress
makes me press the key, so no luck.
我在网络上找不到任何选择.
I've found no option on the web.
推荐答案
我想如果您不想诉诸骇人听闻的方式.
I suppose if you don't want to resort to multithreading (one thread doing the computation in the while loop, another one waiting for input and setting a global sentinel value to break the while loop) you can try to implement breaking the loop on catching a keyboard-interrupt (ctrl-c). This should be possible, albeit in a kinda hackish way.
这篇关于循环按下Matlab中的一个键可停止无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文