Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab interrupt throw exception

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!

like image 643
eykanal Avatar asked May 25 '11 14:05

eykanal


2 Answers

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.

like image 81
abcd Avatar answered Nov 15 '22 08:11

abcd


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.

like image 29
Edric Avatar answered Nov 15 '22 08:11

Edric