Is there a -batch option prior to 2019a, i.e., in 2017a
Show older comments
In version 2019a there is a -batch option for running matlab non-interactively that does not start the desktop. Is there an equivalent option for earlier versions of matlab, e.g., 2017a?
Answers (1)
[ EDIT: the command-line below is wrong. See Edric and Steven's comments for the correct way to emulate batch functionality on older releases. ]
Yes on Linux:
matlab -nodesktop -nodisplay -r myscript.m
On Windows, that syntax can still be used to run batch scripts, but it still opens a minimal terminal window.
.
4 Comments
Edric Ellis
on 3 Mar 2020
That's not quite right - the -r option takes a MATLAB expression, not the script filename. Also, -nodisplay implies -nodesktop, so is not necessary. Finally, -batch quits MATLAB after execution. So, putting it all together, you need this:
$ matlab -nodisplay -r 'try, myscript; catch E, disp(getReport(E)); end, exit'
Steven Lord
on 3 Mar 2020
You probably also want to add the -logfile startup option with the name of a log file as that option's value, so if something does go wrong the output of disp(getReport(E)) gets written to a file you can review later.
Tom Holz
on 3 Mar 2020
Thanks for the corrections. I'd always wondered if -nodisplay and -nodesktop were redundant.
I also like how you handle exceptions to guarantee that exit is called.
Walter Roberson
on 3 Mar 2020
There are a small number of exceptions that cannot be caught: out of memory, and infinite recursion (also, control-c). I do not know what would happen in a -r if one of those was encountered.
On Mac and Linux, one technique is to use < or | i/o redirection to send commands to to standard input, as matlab will continue to read commands from standard input after an exception. For example you could
(echo 'myscript'; echo 'quit') | matlab -nodisplay
If I recall correctly it is also possible to do something similar in Windows.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!