如何在MATLAB中中止程序执行? [英] How can I abort program execution in MATLAB?
问题描述
如何在MATLAB中停止程序执行,而不需要退出MATLAB。我正在寻找像C ++中的exit(1)这样的东西。
How can I stop program execution in MATLAB without exiting MATLAB. I'm looking for something like exit(1) in C++.
我已经尝试退出/退出,但是它们也会杀死MATLAB,这不是我想要的行为。
I've tried exit/quit, but they also kill MATLAB which isn't the behaviour I want.
理想情况下,我会使用try-catch来引发错误,但是我正在修复现有的代码,因为深入嵌套的调用堆栈而无法实现。谢谢!
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!
编辑:
我也尝试过错误
和 return
,但它们最终在调用函数catch块中,这不是我想要的。我只是想停止启动的程序退出。
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.
此外,Ctrl-C要求用户停止执行,这不是我想要的。
Also, Ctrl-C requires the user to stop execution and that's not what I want either.
推荐答案
你想要的是CTRL-C的等效,但是要通过命令执行实际的用户键按。使用Java Robot 来模拟此按键是由@yuk 建议的。 @Pursuit在他的函数中很好地利用了这种方法,这个函数叫做 terminateExecution
。 @MattB提出了另一个基于Java的解决方案, 中断
。
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.
要强制使用 terminateExecution
,我发现有必要调用一个简短的 pause
之后,给Java时间发送键按下,并为MATLAB处理它。所有嵌套的尝试
- catch
语句将被破坏,我想你需要。
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
示例
您返回命令提示符直接从子功能,但它看起来像程序被按键终止:
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();
>>
如果您改用错误
(取消注释在 annoyingFunction
中测试的错误行),它被 killTest 中的
catch
/ code>:
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);
注意:如果您可以输入内容完全退出,只需使用键盘
,并在调试提示符处停止( K>>
)键入 dbquit
,您将返回到基本工作区命令提示符。在 dbquit 的可点击触发器#731932rel =nofollow> MATLAB Central newsreader 。我的版本的解决方案:
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
是和否文本将是可点击的,执行 dbquit
或 dbcont
。
The "Yes" and "No" text will be clickable and will either execute dbquit
or dbcont
.
这篇关于如何在MATLAB中中止程序执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!