Accessing Matlab

To use Matlab, you need to load the appropriate module for the version you want to use. To use the default version, use

$ module load matlab

To use a specific version, check which versions are available with this

$ module available matlab

then load the version you want. Please see our page on Lmod for more information on loading modules.

See the topics in the sidebar to the right for more information about parallel capabilities in Matlab.

Running Matlab interactively

To run Matlab interactively, you need to give Matlab an option supressing the graphical display. To do so, invoke Matlab with

$ matlab -nodisplay

Use of Matlab on the login nodes should be for short, non-resource-intensive tasks only. There are CPU time limits set on the login nodes, so if you try to use Matlab for something long running, your session may be terminated.

You can submit a PBS interactive job request if you need to work within Matlab interactively. For more information on interactive jobs, see the web page on Torque interactive jobs.

Matlab in batch

Matlab assumes that files ending with .m are valid Matlab scripts, and it can run those as if they were Matlab commands if the directory containing them is in the Matlab path. Assume that you have a file, my_script.m, that contains Matlab commands you wish to run. To do so, use

$ matlab -nodisplay -r my_script

If a .m file results in an error, Matlab may drop to an interactive prompt, in which case, Matlab will sit until you type exit. This is OK if you run it interactively, but if the program were running in a job, then it would sit idle until the maximum walltime was exceeded, at which point the batch manager would terminate it and say the job exceeded walltime rather than reporting the Matlab error. For that reason, you may wish to use this form

$ matlab -nodisplay -r "my_script ; exit"

The quotes would also be required if the script name contained a space, regardless of whether you include the exit command.

You can put any list of Matlab commands inside the quotes as a kind of “mini-script”. For example, if you needed to add $HOME/my_toolboxes and all its subdirectories to your Matlab path for it to run and youre username was grundoon, you could use (this would need to be all on one line)

$ matlab -nodisplay -r "addpath(genpath('/home/grundoon/my_toolboxes')) ; (my_script ; exit"

Matlab from PBS

A sample PBS script that shows a single-core Matlab job looks like this:

#PBS -N matlab1
#PBS -q flux
#PBS -l procs=1,mem=1gb,walltime=24:00:00,qos=flux
#PBS -A example_flux
#PBS -m abe


matlab -nodisplay -r my_script

Plotting in Batch Sample

Here are some Matlab commands demonstrating how to save a plot to a color .eps file when running in batch mode.


% save the plot as x_by_y.eps (encapsulated postscript color)
print -depsc x_by_y

% you must include an exit command in your Matlab .m file to close
% Matlab when your program is complete.

Many graphics formats are available, and you can see many more from the Matlab help system with

> help print

Note that your Matlab job will not have any access to a graphical display or terminal, so all of your input must be handled by one .m file (which can call other .m files), and all of your output must be written to either a file or to the standard output stream (what would be the screen, if there were one) by Matlab or saved with the print command. It is slightly faster to write your output to the standard output stream and let PBS capture the results to the job output file. If you send your output to the standard output stream, you can monitor it while it is running using the qpeek command.

Matlab toolboxes available on Flux

The University of Michigan licenses a bundle that includes Matlab and many toolboxes. To see the current list of toolboxes, please see the ITS web page for Matlab licensing. In addition to the toolboxes included in the University bundle, Flux also has licenses for the Matlab Distributed Computing Server.