MATLAB: How to call MATLAB from a Windows command prompt or a Linux Shell and not return control until MATLAB has finished executing

MATLAB

I want to launch MATLAB from a Windows command prompt or a Linux Shell and make sure that control is not returned until MATLAB has finished executing.

Best Answer

On Windows, you can use a batch file that calls MATLAB in order to achieve this. Batch files keep control over the command prompt until all the calls have finished executing.
Below are the steps to do so:
1. Create an empty batch file called, for example, mymatlab.bat
2. The contents of the file should be the following (use the correct path for your MATLAB installation):
echo before
C:\MATLAB\R2008a\bin\win32\matlab.exe %1 %2 %3 %4 %5 %6 %7 %8 %9
echo after
The symbols %1 to %9 represent the number of parameters that can be passed to the batch file, a maximum of 9. So when you call the batch file as follows:
mymatlab.bat -nosplash -nojvm /wait -r "ver;quit"
The actual command that will be executed is:
C:\MATLAB\R2008a\bin\win32\matlab.exe -nosplash -nojvm /wait -r "ver;quit"
Notice that the word "before" appears before MATLAB is launched whereas the word "after" does not appear until MATLAB exits.
----------------------------------
To achieve the same effect on Linux, instead of using % symbols to represent shell script input arguments use the dollar sign ($):
The script should be similar to the code below:
echo before
/myMATLABpath/matlab $1 $2 $3 $4 $5 $6 $7 $8 $9
echo after
Now if you execute the shell script as follows:
/myfolders/myShellScript -nodesktop -nosplash -r "ver;quit"
Then you will notice that the word "before" will be printed in the UNIX shell whereas the word "after" will not appear until after MATLAB exits.
For more information about “MATLAB Startup Options”, refer to the documentation:
web([docroot '/techdoc/matlab_env/f8-4994.html'])