key to end program at any point
2 views (last 30 days)
Show older comments
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 Comment
A. Sawas
on 6 Apr 2019
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"
Answers (1)
Walter Roberson
on 6 Apr 2019
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 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!