Matlab interrupt exception exception

I would like to intercept any calls control- cas an exception, so I deal with interrupts in a less destructive way. The only discussion I found on the Internet was this thread on the matlab exchange since 2009. I was wondering if anyone knew of a new solution that might have arisen in later versions of Matlab. Thank!

+5
source share
2 answers

When you click Ctrl C, MATLAB interprets it as an interrupt. I don’t think there is a way to catch the call and do something like switching to another cycle or so, for example.

However, you can use the function onCleanupto perform operations, such as closing open files, deleting temporary files, recording a log, displaying a message, or even saving a workspace until MATLAB is interrupted. However, it must be called from within the function.

Here is 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 starts and you interrupt it, it returns you to the same folder that you were in when you ran it. It's nice that you are not stuck in some random folder, and you need to enter manually every time.

+7
source

Also, like a method onCleanup, note that you can write your own similar object by getting handleand implementing the method delete. The document is here .

+1
source

All Articles