开发者

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜