Run MATLAB in non-interactive/batch mode, before R2019a

257 views Asked by At

Starting in R2019a, MATLAB has the following command line option:

-batch "statement" Run the specified statement non-interactively.

I need to use R2017a, which does not have this option. So instead I use the -r option:

-r "statement" Automatically run the specified statement immediately after MATLAB starts. This is sometimes referred to as calling MATLAB in batch mode. Files you run must be in the startup folder for MATLAB or on the search path. Do not include path names or file extensions. Enclose the statement in double quotation marks ("statement"). To separate multiple statements, use semicolons or commas.

The problems with this are:

  1. A MATLAB window pops up showing the command(s) executed.
  2. If there is an error in the MATLAB script, the whole thing hangs. The only way I know to make the process end is to issue a quit from within MATLAB, but if the script has an error, then quit is never reached.
  3. If the MATLAB license server cannot be reached, a dialog error pops up, as opposed to returning an exit code.

The solution to 2 is to wrap the body of the script in a "try/catch", but I do not know how to resolve the other issues in R2017a.

I am considering using the MATLAB-Python API, but I would have to do it via Python 3.4 or Python 3.5, which would require my project to have a separate python environment.

1

There are 1 answers

1
mhopeng On

You can use the Matlab Compiler (mcc) to compile your script into a standalone executable. Then you can call the executable, instead of starting matlab to run a script. This method addresses your concerns:

  1. If you are running this on Windows, use mcc -e to suppress a pop-up window.
  2. Applications exit (aka "crash") when they reach an unrecoverable error. Or use appropriate try/catch exceptional handling.
  3. You do not need to start Matlab, so reaching the license server is not an issue. In fact you do not even need to install Matlab to run a standalone executable if you install the MCR.