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:
- A MATLAB window pops up showing the command(s) executed.
- 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
quitfrom within MATLAB, but if the script has an error, thenquitis never reached. - 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.
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:
mcc -eto suppress a pop-up window.