I would like to catch any calls to control-c as an exception, so that I deal with interrupts in a less disruptive way. The only discussion I found online was this thread on the matlab exchange from 2009. I was wondering whether anyone knows of new solution that may have come up in the more recent versions of matlab. Thanks!
When you press CtrlC, MATLAB interprets it as an interrupt. I don't think there is a way for you to catch the call and do something like jump to a different loop or so, for example.
However, you can use the function onCleanup
to do operations like close open file handles, delete temporary files, write a log, display a message, or even save the workspace before MATLAB interrupts. However, it needs to be called from inside a function.
Here's a simple illustrative example
function test
currentDir=pwd;
cd 'path to some folder'
c=onCleanup(@()cd(currentDir));
for i=1:...
%#some computations here
end
So when this function runs and you interrupt, it brings you back to the same folder you were in when you ran it. This is nice to have so that you aren't stuck in some random folder and you need to type in manually every time.
As well as the onCleanup
method, note that you can write your own similar object by deriving from handle
, and implementing a delete
method. The doc is here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With