Matlab:是否可以创建信号处理程序(.m脚本) [英] Matlab: Is it possible to create signal handlers (.m scripts)
问题描述
我已经阅读了文档等内容,但是看不到任何明显的内容.我想有一个信号处理程序,可以拦截^C
,^\
或其他一些按键,这些按键可用于中断长时间运行的脚本(每个离散计算通常为< 1s)并允许其退出优雅地保存当前状态.
I've looked through the documentation, etc, but I'm not seeing anything obvious. I'd like to have a signal handler that can intercept ^C
, ^\
, or some other keypress that could be used to interrupt a long-running script (each discrete computation is typically <1s) and allow it to exit gracefully and save current state.
Matlab确实具有COM的事件处理程序,但它仅适用于Windows,并且我处于* nix环境中.
Matlab does have event handlers for COM, but it's windows-only and I'm in a *nix environment.
如果答案是不走运",我对此很满意……我只是没有看到任何表明我是SOL的东西.
If the answer is 'tough luck', I'm cool with that ... I'm just not seeing anything that says I'm SOL yet.
推荐答案
MATLAB已经将^C
解释为中断.您可以使用onCleanup
对象来确保正确保留程序状态. IE.像这样:
MATLAB already interprets ^C
as an interrupt. You can use onCleanup
objects to ensure that your program state is preserved correctly. I.e. something like:
function testFcn
x = onCleanup( @() disp('perform cleanup here...') );
for ii=1:1000, disp(ii), pause(1), end
执行上述操作,无聊时打^C
.显然,您可以将任何函数句柄连接到onCleanup
对象.另请参见 onCleanup的参考页.
Run the above and hit ^C
when you get bored. Obviously, you can hook any function handle in to your onCleanup
object. See also the reference page for onCleanup.
这篇关于Matlab:是否可以创建信号处理程序(.m脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!