How can I abort program execution in MATLAB?

跟風遠走 提交于 2019-12-30 03:31:08

问题


How can I stop program execution in MATLAB without exiting MATLAB. I'm looking for something like exit(1) in C++.

I've tried exit/quit, but they also kill MATLAB which isn't the behaviour I want.

Ideally I would have used try-catch to bubble up errors, but I'm fixing up existing code and cannot do that due to a deeply nested call stack. Thanks!

EDIT:

I've also tried error and return, but they end up in the invoking functions catch block which isn't what I want either. I simply want to stop the launched program to exit.

Also, Ctrl-C requires the user to stop execution and that's not what I want either.


回答1:


What you want is the equivalent of CTRL-C, but to be executed via a command instead of an actual user key press. Using a Java Robot to simulate this key press was suggested by @yuk. This method was nicely utilized by @Pursuit in his function called terminateExecution. Another Java-based solution, interrupt was proposed by @MattB.

To use terminateExecution robustly, I find it is necessary to call a short pause immediately after to give Java time to send the key press and for MATLAB to handle it. All nested try-catch statements will be broken, as I think you need.

killTest.m

function killTest

try
    annoyingFunction();
    fprintf('Does not run.');
catch ME
    fprintf('Fooled again! (%s)\n',ME.message);
end

end

function annoyingFunction()

somethingWrong = true; % more useful code here
if somethingWrong,
    % error('annoyingFunction:catchableError','catchable error');
    terminateExecution % by Pursuit
    % interrupt % by Matt B.
    pause(0.1)
end

end

Example

You return to the command prompt directly from the subfunction, but it looks like the program was terminated by a key press:

>> killTest
Operation terminated by user during killTest>annoyingFunction (line 17)

In killTest (line 4)
    annoyingFunction();
>>

If you instead use error (uncomment the error line inside annoyingFunction to test), it get's caught by the catch statement in killTest:

>> killTest
Fooled again! (catchable error)

Suggested changes to interrupt (simplifications, more reliable acquisition of command window handle, and readability):

function interrupt

import java.awt.event.KeyEvent
import java.lang.reflection.*

base = com.mathworks.mde.cmdwin.CmdWin.getInstance();
hCmd = base.getComponent(0).getViewport().getView();
cmdwin = handle(hCmd,'CallbackProperties');

argSig = javaArray('java.lang.Class',1);
argSig(1) = java.lang.Class.forName('java.awt.event.KeyEvent');

msTime = (8.64e7 * (now - datenum('1970', 'yyyy')));
args = javaArray('java.lang.Object',1);
args(1) = KeyEvent(cmdwin,KeyEvent.KEY_PRESSED,msTime,...
    KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_C,KeyEvent.CHAR_UNDEFINED);

method = cmdwin.getClass().getDeclaredMethod('processKeyEvent',argSig);
method.setAccessible(true);
method.invoke(cmdwin,args);

Note: If you are OK with typing something to completely quit, just use keyboard and when it stops at the debug prompt (K>>) type dbquit and you will be back to the base workspace command prompt. A cute way to provide a clickable trigger to execute dbquit was provide on the MATLAB Central newsreader. My version of that solution:

fprintf('Terminate execution?\n<a href="matlab: dbquit;">Yes</a> / <a href="matlab: dbcont;">No</a>\n');
keyboard

When this bit of code is run, you get a little prompt like this:

Terminate execution?
Yes / No

The "Yes" and "No" text will be clickable and will either execute dbquit or dbcont.




回答2:


You are looking for Ctrl+c key combination. This will abort any program's execution. Take the cursor to the MATLAB's command window and then press Ctrl+c.

Though there are two scenarios when even Ctrl+c cannot stop the execution:

  1. Sometimes, if a MEX-file is getting executed. Ctrl+c won't have any effect.
  2. If your RAM is so full that it cannot even execute Ctrl+c.

Then you have no other option but to go to Task Manager and stop the MATLAB process.




回答3:


Do you mean

return 

?

You can also use

error("free text argument")

also (as a debugging tool)

keyboard

(but i think that is deprecated)



来源:https://stackoverflow.com/questions/21925137/how-can-i-abort-program-execution-in-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!