How can I interrupt a program in MATLAB?

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

I tried exit / quit, but they also kill MATLAB, which is not the behavior I want.

Ideally, I would use try-catch to generate errors, but I am correcting existing code and cannot do this because of the deeply nested call stack. Thank!

EDIT:

I also tried errorand return, but they fall into catch catch blocks, which are not what I want. I just want to stop the running program to exit.

In addition, Ctrl-C requires the user to stop execution, and this is not what I want.

+4
source share
3

, , CTRL-C, , , . Java Robot @yuk, @Pursuit terminateExecution. Java, interrupt @MattB.

terminateExecution, , pause, Java MATLAB . try - catch , .

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

, , :

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

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

error ( annoyingFunction ), catch killTest:

>> killTest
Fooled again! (catchable error)

interrupt (, ):

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);

. - , keyboard, (K>>), dbquit, . dbquit MATLAB Central newsreader. :

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

, :

Terminate execution?
Yes / No

"" "" dbquit, dbcont.

+3

Ctrl+c. . MATLAB, Ctrl+c.

, Ctrl+c :

  • , MEX . Ctrl+c .
  • , Ctrl+c.

, MATLAB.

+1

You mean

return 

?

You can also use

error("free text argument")

also (as a debugging tool)

keyboard

(but I think it is deprecated)

0
source

Source: https://habr.com/ru/post/1528024/


All Articles