MATLAB Batch Scripting

For certain computations in MATLAB, it is advantageous to create a script to give to MATLAB to perform and wait for the output to be written to a file in your home directory.

This is useful in many situations, in particular if one does not need to do anything interactive but rather if you are trying to perform a large simulation whose output will be produced along the way, or at the very end of the computation. In either case, starting up the graphical front end is unnecessary since one can simply start the job and set things up so that the output is written to a file whose contents you can examine after the computation has completed.

In Unix, say on the department’s compute server LINEAR.BU.EDU (which is accessible via ssh if you have a Mathaccount), or from a Linux desktop if you have one in your office,  you would proceed as follows.

If the script containing the MATLAB code you wish to run is called ‘myscript‘ (in your Math home directory or some sub-directory thereof) then, once you login to LINEAR for example, issue the command
nohup matlab -nosplash  >&! results.txt <  myscript  &


This looks like a mouthful, but the basic idea is that your taking ‘myscript‘ and having MATLAB read it in (where the -nosplash option makes it start without the graphical interface) and process it in ‘batch’ mode which basically means it will evaluate your code and then take any output and write it to a file, in this case ‘results.txt

The nohup command (as well as the & at the end of the line) will make it such that you can log out of the system and MATLAB will keep running and funneling the output to your output file.