在命令行脚本完成之前,如何阻止 MATLAB 返回?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14495/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:38:46  来源:igfitidea点击:

How can I stop MATLAB from returning until after a command-line script completes?

command-linematlab

提问by Joseph Gordon

I see in the MATLAB help (matlab -h) that I can use the -rflag to specify an m-file to run. I notice when I do this, MATLAB seems to start the script, but immediately return. The script processes fine, but the main app has already returned.

我在 MATLAB 帮助 ( matlab -h) 中看到,我可以使用该-r标志来指定m要运行的-file。我注意到当我这样做时,MATLAB 似乎启动了脚本,但立即返回。脚本运行正常,但主应用程序已经返回。

Is there any way to get MATLAB to only return once the command is finished? If you're calling it from a separate program it seems like it's easier to wait on the process than to use a file or sockets to confirm completion.

有没有办法让 MATLAB 只在命令完成后返回?如果您从单独的程序中调用它,似乎等待进程比使用文件或套接字确认完成更容易。

To illustrate, here's a sample function waitHello.m:

为了说明,这里有一个示例函数waitHello.m

function waitHello
    disp('Waiting...');
    pause(3); %pauses 3 seconds
    disp('Hello World');
    quit;

And I try to run this using:

我尝试使用以下方法运行它:

matlab -nosplash -nodesktop -r waitHello

回答by Brian Jorgensen

Quick answer:

快速回答:

matlab -wait -nosplash -nodesktop -r waitHello

In Matlab 7.1 (the version I have) there is an undocumented command line option -wait in matlab.bat. If it doesn't work for your version, you could probably add it in. Here's what I found. The command at the bottom that finally launches matlab is (line 153):

在 Matlab 7.1(我拥有的版本)中,matlab.bat 中有一个未公开的命令行选项 -wait。如果它不适用于您的版本,您可以添加它。这是我发现的。底部最终启动 matlab 的命令是(第 153 行):

start "MATLAB" %START_WAIT% "%MATLAB_BIN_DIR%\%MATLAB_ARCH%\matlab" %MATLAB_ARGS%

The relevant syntax of the start command (see "help start" in cmd.exe) in this case is:

在这种情况下启动命令的相关语法(参见 cmd.exe 中的“帮助启动”)是:

start ["window title"] [/wait] myprogram.exe args ...

A bit higher, among all of the documented command line options, I found (line 60):

在所有记录的命令行选项中,我发现(第 60 行)更高一点:

) else if (%opt%) == (-wait) (
  set START_WAIT=/wait
) else (

So specifying -wait should do what you want, as long as you're also exiting matlab from your script (otherwise it will wait for you to terminate it interactively).

所以指定 -wait 应该做你想做的,只要你也从你的脚本中退出 matlab(否则它会等待你以交互方式终止它)。