# Using Matlab on the cluster

This is a short guide to using Matlab on the cluster. We don't recommend running resource intensive scripts on the login nodes because there are only 2 and other users need to be able to submit jobs and move data on these nodes. If you are running anything resource intensive on the login nodes it may exit prematurely.

## Setup

You will need to choose the version of matlab you wish to use on the cluster. You can use the module avail command to check for different versions:

module avail matlab

And you will note that there are 3 versions (matlab/R2018a, matlab/R2019b, and matlab/R2021bU2), but you will likely want to use the newest version using the module load command:

module load matlab/R2021bU2

If you are using a batch script, you will want to use the appropriate version in that script.

## Execution

### Batch Script

For batch scripts, like this one, you can use this guide to determine the appropriate command line options you need to call your Matlab script, but the most commonly used ones are going to be used in the following matlab execution line:

matlab -nosplash -nodesktop -nodisplay -nojvm -r "myFunction"

Where "myFunction" is a callable function in your Matlab script (with a ".m" extension) or just the name of the script itself. If you want to pass a Bash variable as an argument, simply add parenthesis and the value you wish to use, as in:

someValue=5 #Exclude this line if you want to use SLURM_* variablesmatlab -nosplash -nodesktop -nodisplay -nojvm -r "myFunction(${someValue})" Notably, this works well with sbatch's "--array=" option, where you might want to use${SLURM_ARRAY_TASK_ID} to index some values.

### Interactive

Interactive jobs are possible on the cluster, but there is a possibility of your job stopping if the connection is interrupted. Assuming you are connected with x11 forwarding enabled, using the srun command is the quickest:

module load matlab/R2021bU2srun -c 1 -p shortq -t 00:10:00 --x11 --pty matlab

Note that if you are using x2go, you will have to forward x11 twice to use srun's "--x11" option:

ssh -Y localhostmodule load matlab/R2021bU2srun -c 1 -p shortq -t 00:10:00 --x11 --pty matlab

## Input and Output

With interactive matlab jobs, the output is straight forward (what you see is what you get, plus any files your script/program creates). For batch submission, you have the option of redirecting the standard output and standard error to separate files with the #SBATCH directives (use "%j" to avoid overwriting files without the same jobid):

#SBATCH --output="standardOutput-%j.out"#SBATCH --error="standardError-%j.out"

If you have files input or output to standard windows paths, such as "c:\path\to\file" or "path\to\file", you should change it to match the Linux standard directories using "/" instead of "\" and change "c:\" absolute paths to your own directories' paths like "\${HOME}/path/to/file". Additionally, you will need to upload any files your script will open as input to the cluster.