key to end program at any point
4 次查看(过去 30 天)
显示 更早的评论
i would like to set a specific key (let say the letter a) that will enable termination at any point during the running of m file ,
i tried 'input' but it required immediate response. ' get keywait will also not work .
i am a bit stuck and would be grateful for some guidance .
thanks
1 个评论
A. Sawas
2019-4-6
Wouldn't Ctrl+C do that for you?
To include a callback function that listens to key events, you need to have graphical objects such as Figures. See this page in matlab documentation "Callbacks — Programmed Response to User Action"
回答(1 个)
Walter Roberson
2019-4-6
The only way to do that in MATLAB would be to use some kind of "hotkey" software that detected your key stroke and used the appropriate operating system facility to terminate the MATLAB process, such as Windows "taskkill" or Unix "killall" .
Above, A. Sawas mentioned control-C. Control-C does not meet your requirements for "immediate response". Control-C does not have any effect until the next time that MATLAB checks for interruption. MATLAB does not check for interruption in several circumstances:
- executing inside the symbolic engine
- executing inside the high-speed mathematics libraries
- while waiting for the operating system to swap memory
- while running a .dll or mex file
- while processing calllib()
If MATLAB follows common code safety practices, it would not check for interruptions while doing memory allocation (too much risk of getting inconsistent memory states).
It is not clear whether MATLAB processes interrupts while executing any one "built-in function".
Because of these various times when MATLAB does not check for interruptions, there can be very significant delays before MATLAB responds to control-C -- days even, if you ask MATLAB to do something large and symbolic. The only way to get certain immediate response is to terminate MATLAB (even then, response can take time if MATLAB is busy calling into the operating system and it has locked interrupts to prevent inconsistent state.)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!