Stop using -r to run MATLAB in batch jobs
You have a script, let's call it myscript.m, and you want to run it non-interactively, or as a 'batch-job'. If you are still doing something like this
matlab -nodisplay -nosplash -nodesktop -r "myscript;exit"
or
matlab -nodisplay -nosplash -nodesktop -r "run('myscript.m');exit"
then stop! Since R2019a, MATLAB has had a -batch command line switch and we now recommend that you do the following instead
matlab -nodisplay -batch myscript
If nothing else, the newer syntax looks a lot cleaner and involves less typing. However, let's dig into some additional reasons why you should prefer the newer method.
-batch is less noisy at the beginning
Lets say that myscript.m just contains the following
% Contents of myscript.m
fprintf("Hello from MATLAB\n")
Running this the old way is rather noisy!
matlab -nodisplay -nosplash -nodesktop -r "myscript;exit"
< M A T L A B (R) >
Copyright 1984-2025 The MathWorks, Inc.
R2025b (25.2.0.2998904) 64-bit (maca64)
August 21, 2025
To get started, type doc.
For product information, visit www.mathworks.com.
Hello from MATLAB
Batch mode gets right to the point
matlab -nodisplay -batch myscript
Hello from MATLAB
-batch doesn't need an explicit exit
Anyone who has ever used the old way will have, at some point, forgotten to add 'exit' to the end of the command
matlab -nodisplay -nosplash -nodesktop -r "myscript"
< M A T L A B (R) >
Copyright 1984-2025 The MathWorks, Inc.
R2025b (25.2.0.2998904) 64-bit (maca64)
August 21, 2025
To get started, type doc.
For product information, visit www.mathworks.com.
Hello from MATLAB
>>
MATLAB enters its terminal mode, runs your command and then sits there, waiting for you to type more commands. If you are sat at an interactive terminal and this happens, you shake you head at your forgetfulness, type exit and move on with your life.
If you do this in a non-interactive setting such as on a High Performance Computing (HPC) cluster or in a CI/CD environment, the job will sit there doing nothing until it is (hopefully!) terminated by the scheduler or a timeout.
When you use -batch, MATLAB will always exit when it has completed your script.
-batch exits on error
Inevitably, our code will sometimes exit with an error. Let's force the point with a new script: myBadScript.m
% Contents of myBadScript.m
fprintf("Hello from MATLAB\n")
error("Something has gone very wrong")
If we run this the old way, things don't go the way we'd like them to
matlab -nodisplay -nosplash -nodesktop -r "myBadScript;exit"
< M A T L A B (R) >
Copyright 1984-2025 The MathWorks, Inc.
R2025b (25.2.0.2998904) 64-bit (maca64)
August 21, 2025
To get started, type doc.
For product information, visit www.mathworks.com.
Hello from MATLAB
Error using myBadScript (line 3)
Something has gone very wrong
>>
MATLAB stops at the error so the exit command is never reached and we are stuck with the same problem we had when we forgot the exit command: MATLAB just sits there waiting for input.
If we use -batch, MATLAB terminates gracefully after the error message
matlab -nodisplay -batch myBadScript
Hello from MATLAB
Error using myBadScript (line 3)
Something has gone very wrong
When you use -batch, the process also terminates with a non-zero error code if your script exits with an error. For example, on Linux or Mac we can query the shell variable $? to display the error code returned by the previous command.
matlab -batch myscript
Hello from MATLAB
echo $?
0
matlab -batch myBadScript
Hello from MATLAB
Error using myBadScript (line 3)
Something has gone very wrong
echo $?
1
You can use batchStartupOptionUsed to infer you're running with -batch
Sometimes we want our script to behave one way if it is run interactively and another way if it is running in batch. With -batch there is a straightforward way of doing this via the batchStartupOptionUsed flag. Let's change myscript.m to the following
% Contents of myscript.m
fprintf("Hello from MATLAB\n")
if batchStartupOptionUsed
fprintf("I am running in batch mode\n")
else
fprintf("I think I'm running interactively\n")
end
Run this the old way and batchStartupOptionUsed is false
matlab -nodisplay -nosplash -nodesktop -r "myscript;exit"
< M A T L A B (R) >
Copyright 1984-2025 The MathWorks, Inc.
R2025b (25.2.0.2998904) 64-bit (maca64)
August 21, 2025
To get started, type doc.
For product information, visit www.mathworks.com.
Hello from MATLAB
I think I'm running interactively
I am sure that there are ways to infer exactly how you've started MATLAB and to behave appropriately even using the old way of doing this but batchStartupOptionUsed is just easy and clean.
matlab -nodisplay -batch myscript
Hello from MATLAB
I am running in batch mode
-batch disables toolbox caching and changes to settings
Toolbox caching is normally used to speed up access to toolbox functions. Sounds like faster toolbox functions is a good thing so why is -batch disabling it? This is to ensure that there are no inconsistencies between runs. Caching is user specific and persistent but batch runs are meant to be ephemeral and isolated. Furthermore, Continuous Integration (CI) jobs always run in clean environments so caching wouldn't persist between sessions anyway.
Similar thinking applies to settings. We want to ensure that batch jobs run in a predictable environment, unaffected by user-specific sessions.
-batch could be faster
I wondered if -batch would be faster and although, in theory, it could be; it's not the case yet. As of R2025b, there isn't any compelling speed difference between -batch and the older way of doing things. This may change in the future, however, since -batch mode offers the possibility of not needing to load various things.
So, use -batch for running batch jobs
It's a lot easier to remember than all the other switches you perhaps used to use.
评论
要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。