Skip to main content

Matlab

The Matlab programming environment is available on Hamilton as a software module that must be loaded in order to make the Matlab commands available.  To see which versions are available and load one of them, type e.g:

module avail matlab
module load matlab/R2025a

MATLAB can be used on Hamilton in two ways.

  • The full MATLAB graphical interface can be run on a compute node via the Hamilton portal.  For lighter work, it can also be used on the login nodes by loading a version of the matlab module, as above, and executing the matlab command. If you wish to work this way, we recommend that you connect to Hamilton using X2GO (see the Login page for details) to improve performance and stability.
  • Matlab's command-line interface is used for most intensive Matlab work on Hamilton. It is typically used to submit work to the compute nodes as non-interactive batch jobs, and an example job script is given below.  As batch jobs are not attached to any display device, any graphical output from them must be saved to files.

The command-line interface can also be used for light work on the login nodes.  If you wish to use this, please constrain it by starting it with:
matlab --singleCompThread 

Installing Matlab code and extra toolboxes

If you would like to save a Matlab function in a file, create a matlab folder in your home directory and place .m files in it.

For example, a file $HOME/matlab/f.m could contain the following text that defines a function, f, which can be used from the Matlab prompt:

function y = f(x)
  y = x + 2

Alternatively, if you have downloaded a community-provided Matlab Toolbox, you can make it available to your Matlab session by adding the location of its directory to the MATLABPATH environment variable.

Running Matlab jobs

An example Matlab program, in file my_matlab_program.m, might read:

% Perform a simple calculation:
x = 2;
y = 3;
x + y

The following example job script, my_matlab_job.sh, runs my_matlab_program.m in a batch job using a single CPU core:

#!/bin/bash

# Request resources:
#SBATCH -c 1          # 1 CPU core
#SBATCH --mem=1G      # 1 GB RAM
#SBATCH --time=1:0:0  # 1 hour (days-hours:minutes:seconds)

# Run in the 'shared' queue
# (job will share node with other jobs)
#SBATCH -p shared

# Make Matlab available:
module load matlab/R2021

# Start Matlab:
# (-nodisplay disables the graphics interface)
# (-singleCompThread stops matlab from using more than one core)

matlab -nodisplay -singleCompThread -r "run my_matlab_program.m"

To submit this job to the queue, type the command:

sbatch my_matlab_job.sh

Once your jobs are running, the Hamilton portal can be helpful to monitor performance.

Making Matlab faster

Matlab has a number of ways to parallelise tasks and increase performance. The Matlab profiler can help identify understand which of these techniques is best for your code.