MATLAB sbatch script
How to run MATLAB from a job script
The most common way to use MATLAB on a desktop computer is through its graphical user interface (GUI). MATLAB can also be run in this manner on the supercomputer. To use the MATLAB GUI, visit the dedicated interactive web portal and fill out the form to get started. (See Connecting with the Web Portal to get started running GUI applications).
However, the most powerful way to use MATLAB on the supercomputer is through an sbatch script. This allows MATLAB jobs to be submitted to the scheduler and run "in the background."
To run MATLAB in this manner, after loading the MATLAB module, a few key options have to be passed to the MATLAB executable, along with the MATLAB program script you wish to run. An example of this is shown below:
MATLAB -batch "script"
Here is an explanation of what these options mean:
-batch “script”
Start MATLAB in ‘batch’ mode and run the statement, in this case execute
script.m
This option implicitly turns on many options, such as
-nodesktop
and-nosplash
. See the Mathworks documentation for more information (screenshot below)
Example sbatch script
Below is an example sbatch
script, myjob.sh
, which runs a MATLAB script script_with_plotting.m
using four cores in the htc partition. Note that script_with_plotting.m
is in the same directory as this calling sbatch
script.
#!/bin/bash
#SBATCH -c 4 # number of cores
#SBATCH -t 0-00:02:00 # time in d-hh:mm:ss
#SBATCH -p htc # Partition (default for jobs < 4 hours)
#SBATCH -q normal # QOS (default for main cpu partitions)
#SBATCH -o slurm-%j.out # File to save job's STDOUT to (%j = JobId)
#SBATCH -e slurm-%j.err # File to save job's STDERR to (%j = JobId)
#SBATCH --mail-type=ALL # Send a notification when a job starts, stops, or fails
#SBATCH --mail-user=%u@asu.edu # Send-to address (%u expands to username)
# Always purge modules to ensure a consistent environment
module purge
# Load version of MATLAB
module load MATLAB/2023a
# Run script
MATLAB -batch "script_with_plotting"
The contents of script_with_plotting.m
follow
clear all, close all
axis square, hold on
u = 2*pi*linspace(0,1,100001);
v = exp(1i*u);
y = exp(20*(1i-0.3)*u);
N = 16;
for k = 1:2*N
z = exp(1i*k*pi/N).*y;
plot(real(z),imag(z),'-','LineWidth',1.25)
end
plot(real(v),imag(v),'k-','LineWidth',2)
set(gca,'XColor', 'none','YColor','none')
fig = gcf;
fig.PaperPositionMode = 'auto';
fig_pos = fig.PaperPosition;
fig.PaperSize = [fig_pos(3) fig_pos(4)];
print(fig,'-dpdf','-painters','-r600','-bestfit','spiral.pdf');
Which generates the following spiral plot:
This job may be submitted to the scheduler with the shell command, sbatch myjob.sh
.
More MATLAB Command Line Options
There are many more options and parameters that can be passed to MATLAB to control how it operates. Mathworks provides some documentation here.
More information on MATLAB's command line options and parameters can be found here