如何在命令行脚本完成之后停止MATLAB的返回?

时间:2022-09-04 01:07:38

I see in the MATLAB help (matlab -h) that I can use the -r flag 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文件。我注意到,当我这样做时,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

1 个解决方案

#1


24  

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:

在这种情况下,start命令的相关语法(请参阅cmd.exe中的“help start”)是:

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(否则它将等待你以交互方式终止它)。

#1


24  

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:

在这种情况下,start命令的相关语法(请参阅cmd.exe中的“help start”)是:

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(否则它将等待你以交互方式终止它)。