Matlab: Is it possible to create signal handlers (.m scripts)

心不动则不痛 提交于 2019-12-11 03:18:35

问题


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 does have event handlers for COM, but it's windows-only and I'm in a *nix environment.

If the answer is 'tough luck', I'm cool with that ... I'm just not seeing anything that says I'm SOL yet.


回答1:


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

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.



来源:https://stackoverflow.com/questions/5906967/matlab-is-it-possible-to-create-signal-handlers-m-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!