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

I looked through the documentation, etc., but I do not see anything obvious. I would like to have a signal handler that can intercept ^C , ^\ or another keystroke that can be used to interrupt a long-running script (each discrete calculation is usually <1s) and allows for graceful output and saving the current state.

Matlab has event handlers for COM, but these are just windows, and I'm in a * nix environment.

If the answer is β€œhard luck”, I’m cool with this ... I just don’t see anything that says I am SOL.

+2
source share
1 answer

MATLAB already interprets ^C as an interrupt. You can use onCleanup objects to make sure your program state is saved correctly. That is, something like:

 function testFcn x = onCleanup( @() disp('perform cleanup here...') ); for ii=1:1000, disp(ii), pause(1), end 

Run above and press ^C when you get bored. Obviously, you can bind any function descriptor to your onCleanup object. See also the man page for onCleanup .

+6
source

All Articles