How can MATLAB be used?

In the following we describe two main ways to use MATLAB at Abacus.

  • Non-interactive Slurm script (recommended)
  • Semi-interactive using a MATLAB GUI running on your own computer

Note that MATLAB is currently only available for users from some of the Danish universites. For further information, have a look here.

Running MATLAB via a Slurm script

The recommended way to use MATLAB on Abacus is to run it as a non-interactive Slurm batch script.

To do this your MATLAB code must be able to run successfully from the command line prompt without the use of any graphics, i.e., the following must run successfully (given that your MATLAB code is saved in the file matlab_code.m).

sysop@fe1:~$ module add matlab/R2016a  # Use any of the available MATLAB versions
sysop@fe1:~$ matlab -nodisplay -r matlab_code

Next, to actually run your code, you must copy all the relevant files to Abacus, and next write a Slurm script as shown below. The sample sbatch script can be found on the Abacus frontend node at the location /opt/sys/documentation/sbatch-scripts/matlab/

#! /bin/bash
#SBATCH --nodes 1                 # number of nodes
#SBATCH --time 2:00:00            # max time (HH:MM:SS)

echo Running on "$(hostname)"
echo Available nodes: "$SLURM_NODELIST"
echo Slurm_submit_dir: "$SLURM_SUBMIT_DIR"
echo Start time: "$(date)"

# Load relevant modules
module purge
module add matlab/R2016a

# Run the MATLAB code available in matlab_code.m
# (note the missing .m)
matlab -nodisplay -r matlab_code

echo Done.

Running MATLAB via a MATLAB GUI on your own computer

This guide describes how to use MATLAB on Abacus in combination with a MATLAB GUI running on your own computer/laptop.


Running MATLAB in this way requires a MATLAB MDCS (MATLAB Distributed Computing Server) license. Most users have such a license available (including e.g., SDU users), but some do not (currently e.g., AAU users).

To check whether you have a valid MDCS license available run the following commands. If no errors are shown, you are ready to go. Otherwise you are welcome to contact us at

testuser@fe1:~$ module purge ; module add matlab/R2017a
testuser@fe1:~$ matlab -dmlworker -nodisplay -r exit

                                  < M A T L A B (R) >
                        Copyright 1984-2017 The MathWorks, Inc.
                         R2017a ( 64-bit (glnxa64)
                                   February 23, 2017

To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit


  • Download the configuration files corresponding to the MATLAB version running on your own computer.

  • Unzip/untar the file and place the contents in one of these two locations

    • If you have the right to do this on your computer, use the subfolder toolbox/local inside the main MATLAB program folder. You can use the command matlabroot inside MATLAB to get the root folder of MATLAB, and then add toolbox/local, e.g.,

      • C:\Program Files\MATLAB\R2016a\toolbox\local
      • /Applications/
      • /usr/local/matlab/r2016a/toolbox/local
    • Otherwise use the folder returned by the command userpath inside MATLAB (usually My Documents\MATLAB or Documents\MATLAB).

  • Start/restart MATLAB.

  • Configure MATLAB to run parallel jobs on the SDU cluster by calling configCluster. configCluster only needs to be called once per version of MATLAB (e.g. R2015a, R2015b, etc.)

>> configCluster
Username on ABACUS (e.g. joe): testuser
Clearing all ClusterInfo settings.

Before submitting a job to ABACUS, you must specify the account name.

        >> % E.g. set account name to sdutest_slim
        >> ClusterInfo.setProjectName('sdutest_slim')

Before submitting a job to ABACUS, you must specify the wall time.

        >> % E.g. set wall time to 1 hour
        >> ClusterInfo.setWallTime('01:00:00')

>> ClusterInfo.setProjectName('sdutest_slim')
>> ClusterInfo.setWallTime('01:00:00')
>> % Jobs will now default to the cluster rather than running locally

Note that the projectName as specified in ClusterInfo.setProjectName('sdutest_slim') is actually the Slurm account name, and must contain the node type as a suffix, i.e., _slim, _gpu, or _fat.


At Abacus we only support access using ssh keys. The first time you submit a job to the cluster, you are asked for the location of your ssh private key file (usually id_rsa found in ~/.ssh - see our guide on how to use SSH keys). You are also asked for the passphrase of the key.

Both the user name and the location of the private key are stored with MATLAB so that you are not prompted for it at a later time. The passphrase is not saved, i.e., you are asked each time you start a new MATLAB session. If your key does not have a passphrase, you simply leave the passphrase field blank when asked.

Where is your ssh private key located?

Serial jobs

Use the batch command to submit asynchronous jobs to the cluster. The batch command will return a job object which is used to access the output of the submitted job. See the example below and see the MATLAB documentation for more help on batch.

Note: In the example below, wait is used to ensure that the job has completed before requesting results. In regular use, one would not use wait, since a job might take an elongated period of time, and the MATLAB session can be used for other work while the submitted job executes.

>> % Get a handle to the cluster
>> c = parcluster;
>> % Submit a batch job to query where MATLAB is running on the cluster
>> % The first time you do this, you are asked for your credentials (see above)
>> j = c.batch(@pwd, 1, {});

additionalSubmitArgs =

-n 1 -A sdutest_slim -t 01:00:00 --licenses=matlab:1

>> % Wait for the job to finish before querying for results
>> j.wait
>> % Now that the job has completed, fetch the results
>> j.fetchOutputs{:}
Warning: Execution of Task with ID 1 generated warnings. List warnings

ans =


>> % No longer need the results, so delete the job
>> j.delete

(You may get a warning when executing j.fetchOutputs{:}. This can be ignored.)

To retrieve a list of currently running or completed jobs, call parcluster to retrieve the cluster object. The cluster object stores an array of jobs that were run, are running, or are queued to run. This allows us to fetch the results of completed jobs. Retrieve and view the list of jobs as shown below.

>> % Retrieve the results of past jobs from the cluster
>> jobs = c.Jobs

jobs = 

 4x1 Job array:

         ID           Type        State       FinishTime  Username  Tasks
    1     1    independent     finished                -  testuser      1
    2     2    independent     finished                -  testuser      1
    3     3    independent     finished                -  testuser      1
    4     5           pool     finished                -  testuser     13
    5    10           pool     finished                -  testuser      9
    6    11           pool     finished                -  testuser     17
    7    12    independent       queued                   testuser      1

Once we've identified the job we want, we can retrieve the results as we've done previously. If the job produces an error, we can call the getDebugLog method to view the error log file. The error log can be lengthy and is not shown here. The example below will retrieve the results of job #3.

NOTE: fetchOutputs is used to retrieve function output arguments. Data that has been written to files on the cluster needs be retrieved directly from the file system.

>> % Retrieve the results from the 3rd job
>> j3 = jobs(3);
>> j3.fetchOutputs{:}
Warning: Execution of Task with ID 1 generated warnings. List warnings

ans =


>> % For debugging, retrieve the output/error log file
>> j3.Parent.getDebugLog(j3.Tasks(1))
Executing: /opt/sys/apps/matlab/R2016a/bin/worker

                            < M A T L A B (R) >
                  Copyright 1984-2016 The MathWorks, Inc.
                   R2016a ( 64-bit (glnxa64)
                             February 11, 2016

To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit

    Academic License

2016-08-09 12:34:56 | About to exit MATLAB normally
2016-08-09 12:34:56 | About to exit with code: 0


Parallel jobs

You can also submit parallel workflows with batch. Let's use the following example for a parallel job. The file is also available on the cluster as /opt/sys/documentation/sbatch-scripts/matlab/parallel_example.m

% parallel_example.m

function t = parallel_example(iter)

if nargin==0, iter = 16; end

disp('Start sim');

t0 = tic;
parfor idx = 1:iter
    A(idx) = idx;
t = toc(t0);

disp('Sim completed');

We'll use the batch command again, but since we're running a parallel job, we'll also specify a MATLAB Pool.

>> % 8 workers for 16 sims
>> j = c.batch(@parallel_example, 1, {}, 'pool', 8);

additionalSubmitArgs =

-n 9 -A sysops_workq -t 01:00:00 --licenses=matlab:9

>> % Wait for the job to finish before querying for results
>> j.wait
>> % Now that the job has completed, fetch the results
>> j.fetchOutputs{:}

ans =



The job ran in 4.66 seconds using eight workers. Note that these jobs will always request N+1 CPU cores, since one worker is required to manage the batch job and pool of workers. For example, a job that needs eight workers will consume nine CPU cores.

We'll run the same simulation, but increase the pool size. Note, for some applications, there will be a diminishing return when allocating too many workers. This time, to retrieve the results at a later time, we'll keep track of the job ID.

>> % 16 workers for 16 sims
>> j = c.batch(@parallel_example, 1, {}, 'pool', 16);

additionalSubmitArgs =

-n 17 -A sysops_workq -t 0-1 --licenses=matlab:17

>> % Get the job ID so that we can retrieve the results of the job after quitting
>> id = j.ID

id =


>> % clear the "j" variable as if we quit MATLAB
>> clear j

Once we have a handle to the cluster, we can later call the findJob method to search for the job with the specified job ID.

>> % Get a handle to the cluster
>> c = parcluster;
>> % Find the old job
>> j = c.findJob('ID', 5);
>> % Check that the state is finished
>> j.State

ans =


>> % Now that the job has completed, fetch the results
>> j.fetchOutputs{:}

ans =


>> % For debugging, retrieve the output/error log file (not included here)
>> j.Parent.getDebugLog(j)

The job now runs in 2.42 seconds using 16 workers. Run the code with different numbers of workers to determine the ideal number to use.

Alternatively, to retrieve job results via a graphical user interface, use the Job Monitor, which can be found under Parallel | Monitor Jobs as shown below.

Job monitor

Configuring jobs

Prior to submitting the job, we can specify:

  • Account/project, e.g. sdutest_slim
  • Email Notification (when the job is running, exiting, or aborting), and
  • Wall time

Specification is done with ClusterInfo. The ClusterInfo class supports tab completion to ease recollection of method names.

>> % configure job: Account, Email Notification, and Wall Time
>> c = parcluster;
>> % use sdutest_slim account
>> ClusterInfo.setProjectName('sdutest_slim')
>> % Specify email notification and end of job
>> % (you can also use e.g., ALL, see --mail-type in man sbatch)
>> ClusterInfo.setEmailNotification('END')
>> % Request 2 hours of wall time
>> ClusterInfo.setWallTime('02:00:00')
>> % 8 workers for 32 sims
>> j = c.batch(@parallel_example, 1, {32}, 'pool', 8);

additionalSubmitArgs =

-n 9 -A sdutest_slim -t 02:00:00 --mail-type="END" --licenses=matlab:9

Any parameters set with ClusterInfo will be persistent both between jobs and MATLAB sessions. To see the values of the current configuration options, call the state method. To clear a value, assign the property the appropriate empty value ('', [], or false).

>> ClusterInfo.state

                          Arch : 
                   ClusterHost : 
               DataParallelism : 
                     DiskSpace : 
             EmailNotification : END
                   GpusPerNode : 
                      MemUsage : 
                PrivateKeyFile : /Users/testuser/.ssh/id_rsa
   PrivateKeyFileHasPassPhrase : 1
                  ProcsPerNode : 
                   ProjectName : sdutest_slim
                     QueueName : 
          RequireExclusiveNode : 0
                   Reservation : 
                       SshPort : 
                        UseGpu : 0
            UserDefinedOptions : 
             UserNameOnCluster : testuser
                      WallTime : 02:00:00
>> % Turn off email notification
>> ClusterInfo.setEmailNotification('')

To learn more

To learn more about the MATLAB Parallel Computing Toolbox, check out these resources from MathWorks:

MATLAB Hosting Provider Agreement

Abacus 2.0 has a Hosting Provider Agreement with MathWorks that allows us to install MATLAB on our system and have users provide their own licence to access it. In particular, if your university has a licence for MATLAB that you can use on the computer in your office then it will most likely be possible to use it on our systems.

To do this you need to checkout a licence directly from your university's licence servers - contact us at to determine if this has already been setup. Please use your official university e-mail address for this.

If it’s not already set up, we will ask you to contact your licence administrator to ask them to provide us with the details of their licence server.

Currently we have a setup for users from AU, AAU, DTU, KU, and SDU.