Software name: 

MATLAB is available to all users at HPC2N.


MATLAB is a numerical computing environment and fourth generation programming language.


MATLAB is a numerical computing environment and fourth generation programming language. Developed by The MathWorks, MATLAB allows matrix manipulation, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs in other languages. Although it is numeric only, an optional toolbox uses the MuPAD symbolic engine, allowing access to computer algebra capabilities.


MATLAB is available on all our systems.


Umeå University have signed a “Third party access rider to the Mathworks, Inc. Software license agreement”. The rider allows Third Parties to use all licensed programs, provided such access and use is solely for the purpose of academic course work and teaching, noncommercial academic research, and personal use which is not for any commercial or other organizational use.

If you work at a non academic organization or need toolboxes not included in the Umeå University license but have your own license, please contact and we will help you find out if you can use Matlab at HPC2N using that license.

SNIC has bought 500 licenses for Matlab Distributed Computing Server (MDCS) available to all SNIC users. 

Available toolboxes

Available toolboxes may shift from time to time. To get a list of all currently available toolboxes use the 'ver' command from within MATLAB. 

Usage at HPC2N 

MATLAB is available as a module. To see which versions are available use:

module spider matlab

Read the page about modules to see how to load the required module.

Before starting MATLAB the first time

Matlab uses a hidden directory named .matlab in your home directory to store application state and settings. The first time you start Matlab, it will create this hidden directory in your home directory (~/.matlab). Since your home directory resides on the AFS namespace, your running jobs and consequently Matlab will have limited permissions, sometimes causing your job to fail. To resolve this, you need to move the directory to the parallel file system 'pfs' where Matlab has full permissions, and then provide a link to the directory so Matlab can find it. Log in to one of the HPC2N resources and run the following commands, before starting MATLAB for the first time:

rm -rf $HOME/.matlab
mkdir /pfs/nobackup$HOME/.matlab
ln -s /pfs/nobackup$HOME/.matlab $HOME

Note: If you have run MATLAB previously at HPC2N, without moving your .matlab directory, the commands above will remove your current settings.

Starting MATLAB

Matlab can be used in a couple of different ways:

  1. Using the MATLAB Desktop/graphical interface. (Recommended Use)
    Use either A or B:
    1. Login to HPC2N using ThinLinc. For more information see our Running ThinLinc Guide
      1. Open a terminal window: Applications -> System Tools -> MATE Terminal
    2. Login to HPC2N with X11 forwarding enabled (See also our guide for Connecting from Windows
      ssh -Y
      ssh -Y
  2. Text mode: If you don't want to or have the possibility to run the MATLAB Desktop, you can run MATLAB in text-mode
    ​Login as usual to HPC2N
  3. In batch-scripts. See below Using MATLAB in batch-scripts (Not recommended anymore)

After logging in, load the MATLAB module and start MATLAB:

# Check for available versions
module spider matlab
# Select required version
# Since different versions of MATLAB differ in user configuration it is important to specify which version to use
module load MATLAB/2019a.01
cd /pfs/nobackup$HOME
matlab -singleCompThread


  1. As interactive use of MATLAB is usually done on shared login-nodes, excessive use of MATLAB will prevent other users from using the resources. By default MATLAB use as many threads (cores) it possibly can.
    On the login-nodes MATLAB MUST be started with the option '-singleCompThread', preventing MATLAB from using more than one thread..
    This will NOT prevent MATLAB from using the MATLAB Distributed Computing Server (MDCS) with which any number of cores can be used for computations.
  2. If running MATLAB in text-mode, add '-nodesktop' when starting MATLAB.
  3. As usual, you need to be in the parallel filesystem, 'pfs', to be able to write during batch jobs, so it is recommended to start MATLAB in 'pfs'.

Configuration and setup

Configuration and setup of MATLAB is described here: "Configure 2018b and later".

Serial batch jobs

To run serial MATLAB jobs on the cluster you first needs to define a cluster object and then submit it using the batch command:

% Get a handle to the cluster
% See the page for configuring and setup of MATLAB 2018b for details
% myfcn is a command or serial MATLAB program.
% N is the number of output arguments from the evaluated function
% x1, x2, x3,... are the input arguments
j = c.batch(@myfcn, N, {x1,x2,x3,...})

To query the state of the submitted job use:

% Query the state of the job
% Wait for the job to finish (blocking though so you can not use MATLAB for anything else)

After the job has finished you can fetch the output using:

% If the state of the job is finished, fetch the result
% when you do not need the result anymore, delete the job

If you are running a lot of jobs or if you want to quit MATLAB and restart it at a later time you can retrive the list of jobs:

% Create a handle to the cluster
% See the page for configuring and setup of MATLAB 2018b for details
% Get the list of jobs
jobs = c.Jobs
% Retrive the output of the second job
output = j2.fetchOutputs{:}

Note: If calling batch from a script, use load instead of fetchOutputs.

Parallel batch jobs

Running parallel batch jobs are quite similiar to running serial jobs, we just need to specify a MATLAB Pool to use and of course MATLAB code that are parallized. This is easiest illustrated with an example:

function t = parallel_example(iter) 
t0 = tic; 

parfor idx = 1:iter 
    A(idx) = idx; 

t = toc(t0);

We will run the example on 4 cores:

% Get a handle to the cluster
% See the page for configuring and setup of MATLAB 2018b for details
% Run the jobs on 4 workers
j = c.batch(@parallel_example, 1, {16}, 'pool', 4)
% Wait till the job has finished. Use j.State if you just want to poll the
% status and be able to do other things while waiting for the job to finish.
% Fetch the result after the job has finished
ans =


  • Running parallel jobs in MATLAB always requires N+1 CPUs as one worker is required to keep track of the batch job and the pool of workers. I.e if you want to keep your job on one node, you should only use number of cores per node minus one.
  • Increasing the number of workers does not always mean that your job will run faster. Overhead increases and will make the total computation time longer, when using many workers.

Another way to get the result from a job at a later time is to keep track of the job ID:

% Get a handle to the cluster
% See the page for configuring and setup of MATLAB 2018b for details
% Run the jobs on 4 workers
j = c.batch(@parallel_example, 1, {16}, 'pool', 8) ;
% get the jobid
id = j.ID

id =
% Clear the job variable. Same as we quit MATLAB
clear j;

Later in another MATLAB session:

% Get a handle to the cluster
% See the page for configuring and setup of MATLAB 2018b for details
% Find the job ID we wrote down
j=c.findJob('ID', 26);

ans =

ans =

If you are running MATLAB Desktop, you can use the Job Monitor (Parallel -> Monitor Jobs) to view the current state of your jobs. 


Sometimes the jobs produce errors, the errors can be retrived with:


For full documentation about running parallel jobs in Matlab please read Mathworks Parallel Computing Toolbox documentation.

Using MATLAB in batch scripts

MATLAB can also be used in batch scripts, though this is not anything we recommend anymore.

The submit-file below runs a serial MATLAB job:

# Change to your actual SNIC project number
# Asking for 1 core
#SBATCH -n 1
#SBATCH -t 00:30:00
#SBATCH --error=matlab_%J.err
#SBATCH --output=matlab_%J.out

# Clean the environment from previously loaded modules
module purge > /dev/null 2>&1

# May need to be changed, depending on resource and MATLAB version to be used
# to find out available versions: module spider matlab
module add MATLAB/2019a.01

# Local work-around for matlab bug
export MATLAB_PREFDIR="/scratch/$USER-matlab-$$/"

# Executing the matlab program monte_carlo_pi.m for the value n=100000 
# (n is number of steps - see program). 
# The command 'time' is timing the execution 
time matlab -nojvm -nodisplay -r "monte_carlo_pi(100000)"

The submit file and the MATLAB code is available for download: monte_carlo.sbatch, monte_carlo_pi.m

Submit with

sbatch monte_carlo.sbatch
Updated: 2020-07-14, 21:10