The below guide helps you to run MATLAB on Discovery.

This can be carried out in three different ways:

  • Using Open OnDemand.

  • Running MATLAB interactively using srun.

  • Running MATLAB scripts using SBATCH.

  • Launching the MATLAB GUI using X11 Forwarding.

Running MATLAB

Using Open OnDemand(Easier and Faster way)

  • Open OnDemand provides an easier and faster way of launching MATLAB GUI through an interactive session on the compute node.

  • Users simply need to fill a form in OnDemand web portal to request the resources and launch MATLAB, thus eliminating the need of running a series of bash commands to run MATLAB from the terminal.

  • For more information on how to launch MATLAB via Open OnDemand, refer the link → Running MATLAB using Open OnDemand.

Running MATLAB Interactively Using srun

  1. Login to discovery and execute the below command:

    srun -p normal --cpus-per-task 1 --mem-per-cpu 10g --ntasks=1 --time=0-1:10:00 --pty bash


    srun: job 250774 queued and waiting for resources
    srun: job 250774 has been allocated resources
    [crushev@discovery-c2 ~]$

    On Line number 3, you can infer that the hostname has been changed from discovery-l1 login node to discovery-c2 which is one of the Discovery’s compute nodes.

    For more information about the interactive jobs using srun, please refer to →Interactive Jobs using srun page.

  2. Then, load the matlab module into the working environment. You can load any version of the matlab module as you wish. Please refer to → Module Environments in Discovery page for more information on how to load and search for modules.

    module load matlab/2020a

    To verify that the module matlab has been loaded successfully into the environment, run the module list command.

  3. Now, launch the matlab module with graphical display in disabled mode.

    matlab -nodisplay


                                   < M A T L A B (R) >
                             Copyright 1984-2020 The MathWorks, Inc.
                            R2020a Update 3 ( 64-bit (glnxa64)
                                     May 27, 2020
    To get started, type doc.
    For product information, visit

    You can run the MATLAB commands now and start performing your computations using command line interface.

However, you can only access and run programs in MATLAB until the Walltime you specified has been reached.

Running MATLAB Scripts Using SBATCH

  1. Create a new folder called matlab in your home directory.

    mkdir matlab && cd matlab
  2. Create the script file and the MATLAB file sample.m and then copy paste the codes in and sample.m tabs respectively.

    vi sample.m

    • sample.m

    #SBATCH --output output-1.out
    #SBATCH --ntasks 1
    #SBATCH --cpus-per-task 1
    #SBATCH --mem-per-cpu 4G
    #SBATCH --time 00:10:00
    #SBATCH --partition normal
    #Load matlab module
    module load matlab/2020a
    #Specify job steps to run
    srun matlab -batch "sample"
    echo "Job Completed Successfully"

    The resources requested are 1 CPU, 4 GB memory per CPU, 10 minutes of Walltime, and ntasks as 1. Next, load the matlab module and specify the srun command to run the job on the compute nodes.

    %Calculating total number of people
    NoOfStudents = 6000;
    TeachingStaff = 150;
    NonTeachingStaff = 20;
    Total = NoOfStudents + TeachingStaff + NonTeachingStaff;
    fprintf('The total people equals %d\n',Total);
    %To close the matlab after the above script is executed

    + The above code calculates the total number of people by performing the addition of three numbers.

    The MATLAB code must contain quit command at the end to close MATLAB. Otherwise, MATLAB may wait for the further input until the end of the Walltime, wasting the computing resources unnecessarily.

  3. Now, both the files are ready and submit the batch script with the following command:


    After the job has been submitted, you should get an output similar to the one below but with a different job id.

    Submitted batch job 253901

    You can use the command below to check the progress of your submitted job in the queue.

    Syntax: squeue -u <username>

    squeue -u crushev


    253902     normal      script.s  crushev  R       0:02      1 discovery-c11
  4. Once your job has completed and no longer in the queue, you can run the ls command to show the list of files in your working directory.

    output-1.out  sample.m

    Now you’d notice that there is a new file output-1.out and if you view its output with the cat command, you should see something similar to the output below.

    cat output-1.out
                        < M A T L A B (R) >
                Copyright 1984-2018 The MathWorks, Inc.
          R2018a Update 6 ( 64-bit (glnxa64)
                      September 5, 2018
     To get started, type one of these: helpwin, helpdesk, or demo.
     For product information, visit
     The total people equals 6170
     Job Completed Successfully

Launching the MATLAB GUI with X11 Forwarding

It’s possible to access MATLAB’s GUI on Discovery by using X11 Forwarding. This allows you directly access your scripts on Discovery as well as executing your scripts/programs on Discovery’s compute nodes.

First, you need to make sure you have x11 installed and running on your local machine by following the steps below.

  1. Ensure that X11 server is running on your local machine. See how to do this here → Run X11 Server

  2. Log in to Discovery with X11 enabled using the CLI or putty. To see how to do this, visit → Connecting to Discovery with X11-Forwarding enabled

  3. Set up the OpenGL Remote Acceleration environmental variable on the remote server(Discovery). To see how to do this, visit → Enable OpenGL Remote Acceleration

  4. Create a directory called matlabProject and navigate to it

    mkdir matlabProject && cd matlabProject
  5. Enable X11 forwarding and run commands interactively by using the srun command and launching a login shell on Discovery’s compute node.

    srun -p normal --hint=nomultithread --cpus-per-task 1 --mem-per-cpu 10g --ntasks=1 --time=0-1:10:00 --x11 --pty bash


    srun: job 250724 queued and waiting for resources
    srun: job 250724 has been allocated resources
    [vaduaka@discovery-c1 matlabProject]$

    If you notice line 3, the hostname got changed from @discovery-l1 (Login node) to @discovery-c1 (compute node) and logged into one of the Discovery’s compute nodes successfully.

  6. Next, to launch MATLAB GUI, run the matlab command.

    [vaduaka@discovery-c1 matlabProject]$ matlab



    From the output above, you can see that MATLAB’s GUI was successfully launched via x11 forwarding. However, the window will remain accessible for you to run your programs as long as the Walltime you specified hasn’t elapsed.