How can I abort program execution in MATLAB?

后端 未结 3 731
眼角桃花
眼角桃花 2021-01-01 04:10

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 w

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 04:21

    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?\nYes / No\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.

提交回复
热议问题