windows 如何在 Matlab 中停止正在运行的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4791144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to stop a running script in Matlab
提问by Yin Zhu
I write a long running script in Matlab, e.g.
我在 Matlab 中编写了一个长时间运行的脚本,例如
tic;
d = rand(5000);
[a,b,c] = svd(d);
toc;
It seems running forever. Becasue I press F5 in the editor window. So I cannot press C-Break to stop in the Matlab console.
似乎永远运行。因为我在编辑器窗口中按 F5。所以我不能按 C-Break 停止在 Matlab 控制台中。
I just want to know how to stop the script. I am current use Task Manager to kill Matlab, which is really silly.
我只想知道如何停止脚本。我目前使用任务管理器杀死Matlab,这真的很傻。
Thanks.
谢谢。
回答by S Krishna Kumar
Matlab help says this- For M-files that run a long time, or that call built-ins or MEX-files that run a long time, Ctrl+C does not always effectively stop execution. Typically, this happens on Microsoft Windows platforms rather than UNIX[1] platforms. If you experience this problem, you can help MATLAB break execution by including a drawnow, pause, or getframe function in your M-file, for example, within a large loop. Note that Ctrl+C might be less responsive if you started MATLAB with the -nodesktop option.
Matlab 帮助说明了这一点 - 对于长时间运行的 M 文件,或者调用内置程序或运行很长时间的 MEX 文件,Ctrl+C 并不总是有效地停止执行。通常,这发生在 Microsoft Windows 平台而不是 UNIX[1] 平台上。如果您遇到此问题,您可以通过在 M 文件中包含一个 drawow、pause 或 getframe 函数来帮助 MATLAB 中断执行,例如,在一个大循环中。请注意,如果您使用 -nodesktop 选项启动 MATLAB,则 Ctrl+C 的响应速度可能会降低。
So I don't think any option exist. This happens with many matlab functions that are complex. Either we have to wait or don't use them!.
所以我认为不存在任何选择。许多复杂的 matlab 函数都会发生这种情况。要么我们必须等待,要么不要使用它们!
回答by Mach
If ctrl+c doesn't respond right away because your script is too long/complex, hold it.
如果 ctrl+c 没有立即响应,因为您的脚本太长/太复杂,请按住它。
The break command doesn't run when matlab is executing some of its deeper scripts, and either it won't log a ctrl sequence in the buffer, or it clears the buffer just before or just after it completes those pieces of code. In either case, when matlab returns to execute more of your script, it will recognize that you are holding ctrl+c and terminate.
当 matlab 执行一些更深层次的脚本时,break 命令不会运行,它要么不会在缓冲区中记录 ctrl 序列,要么在完成这些代码片段之前或之后清除缓冲区。无论哪种情况,当 matlab 返回执行更多脚本时,它都会识别出您正在按住 ctrl+c 并终止。
For longer running programs, I usually try to find a good place to provide a status update and I always accompany that with some measure of time using tic and toc. Depending on what I am doing, I might use run time, segment time, some kind of average, etc...
对于运行时间较长的程序,我通常会尝试找到一个提供状态更新的好地方,并且我总是在一段时间内使用 tic 和 toc。根据我在做什么,我可能会使用运行时间、段时间、某种平均值等......
For really long running programs, I found this to be exceptionally useful http://www.mathworks.com/matlabcentral/fileexchange/16649-send-text-message-to-cell-phone/content/send_text_message.m
对于真正长时间运行的程序,我发现这非常有用 http://www.mathworks.com/matlabcentral/fileexchange/16649-send-text-message-to-cell-phone/content/send_text_message.m
but it looks like they have some newer functions for this too.
但看起来他们也有一些更新的功能。
回答by Memming
MATLAB doesn't respond to Ctrl-C while executing a mex implemented function such as svd. Also when MATLAB is allocating big chunk of memory it doesn't respond. A good practice is to always run your functions for small amount of data, and when all test passes run it for actual scale. When time is an issue, you would want to analyze how much time each segment of code runs as well as their rough time complexity.
MATLAB 在执行 mex 实现的函数(如 svd)时不响应 Ctrl-C。此外,当 MATLAB 分配大块内存时,它没有响应。一个好的做法是始终针对少量数据运行您的函数,并在所有测试通过后针对实际规模运行它。当时间成为问题时,您需要分析每段代码运行的时间以及它们的粗略时间复杂度。
回答by Rem
Consider having multiple matlab sessions. Keep the main session window (the pretty one with all the colours, file manager, command history, workspace, editor etc.) for running stuff that you know will terminate.
考虑有多个 matlab 会话。保留主会话窗口(具有所有颜色、文件管理器、命令历史、工作区、编辑器等的漂亮窗口)以运行您知道将终止的内容。
Stuff that you are experimenting with, say you are messing with ode suite and you get lots of warnings: matrix singular, because you altered some parameter and didn't predict what would happen, run in a separate session:
您正在试验的东西,假设您正在使用 ode 套件并收到很多警告:矩阵奇异,因为您更改了一些参数并且没有预测会发生什么,请在单独的会话中运行:
dos('matlab -automation -r &')
You can kill that without having to restart the whole of Matlab.
您可以终止它而无需重新启动整个 Matlab。
回答by KitsuneYMG
One solution I adopted--for use with java code, but the concept is the same with mexFunctions, just messier--is to return a FutureValue and then loop while FutureValue.finished() or whatever returns true. The actual code executes in another thread/process. Wrapping a try,catch around that and a FutureValue.cancel() in the catch block works for me.
我采用的一种解决方案——用于 java 代码,但概念与 mexFunctions 相同,只是更混乱——是返回一个 FutureValue,然后在 FutureValue.finished() 或任何返回 true 时循环。实际代码在另一个线程/进程中执行。在 catch 块中包装一个 try、catch 和一个 FutureValue.cancel() 对我有用。
In the case of mex functions, you will need to return somesort of pointer (as an int) that points to a struct/object that has all the data you need (native thread handler, bool for complete etc). In the case of a built in mexFunction, your mexFunction will most likely need to call that mexFunction in the separate thread. Mex functions are just DLLs/shared objects after all.
在 mex 函数的情况下,您将需要返回某种指针(作为 int),该指针指向具有您需要的所有数据的结构/对象(本机线程处理程序、完整的 bool 等)。在内置 mexFunction 的情况下,您的 mexFunction 很可能需要在单独的线程中调用该 mexFunction。毕竟,Mex 函数只是 DLL/共享对象。
PseudoCode
伪代码
FV = mexLongProcessInAnotherThread();
try
while ~mexIsDone(FV);
java.lang.Thread.sleep(100); %pause has a memory leak
drawnow; %allow stdout/err from mex to display in command window
end
catch
mexCancel(FV);
end
回答by Parthian Shot
Since you mentioned Task Manager, I'll guess you're using Windows. Assuming you're running your script within the editor, if you aren't opposed to quitting the editor at the same time as quitting the running program, the keyboard shortcut to end a process is:
既然你提到了任务管理器,我猜你正在使用 Windows。假设您在编辑器中运行脚本,如果您不反对在退出正在运行的程序的同时退出编辑器,则结束进程的键盘快捷键是:
Alt+ F4
Alt+ F4
(By which I mean press the 'Alt' and 'F4' keys on your keyboard simultaneously.)
(我的意思是同时按下键盘上的“Alt”和“F4”键。)
Alternatively, as mentioned in other answers,
或者,如其他答案中所述,
Ctrl+ C
Ctrl+ C
should also work, but will not quit the editor.
也应该工作,但不会退出编辑器。
回答by mehdi SH
if you are running your matlab on linux, you can terminate the matlab by command in linux consule. first you should find the PID number of matlab by this code:
如果您在 linux 上运行 matlab,则可以通过 linux consule 中的命令终止 matlab。首先你应该通过这段代码找到matlab的PID号:
top
最佳
then you can use this code to kill matlab: kill
那么你可以使用这个代码杀死matlab:kill
example: kill 58056
示例:杀死 58056
回答by Ursa Major
To add on:
添加:
you can insert a time check within a loop with intensive or possible deadlock, ie.
您可以在具有密集或可能死锁的循环中插入时间检查,即。
:
section_toc_conditionalBreakOff;
:
where within this section
在本节中
if (toc > timeRequiredToBreakOff) % time conditional break off
return;
% other options may be:
% 1. display intermediate values with pause;
% 2. exit; % in some cases, extreme : kill/ quit matlab
end