What is MATLAB?

MATLAB is a technical programming environment for high-performance numeric computation and visualization. MATLAB integrates numerical analysis, matrix computation, signal processing, and graphics in an easy-to-use environment where problems and solutions are expressed just as they are written mathematically–without traditional programming.

Availability

The University of Hong Kong has a MATLAB Campus-Wide License arrangement to provides unlimited use of  MATLAB and Simulink software to all faculty, staff and students. The software can be installed on the on-campus computing facilities, laboratories, classrooms and research centres, as well as faculty-owned and student-owned personal computers.

Staff and students can access HKU’s MATLAB Portal to download, install and activate the MATLAB license. MATLAB is installed at HPC cluster systems. Staff/students with valid HKU Portal UID can apply an HPC account to run MATLAB on the system.

Toolboxes available

Under the MATLAB Campus-Wide license, it covers the access to the most up-to-date suite of MATLAB and Simulink products by all staff and students. Below are some toolboxes that are commonly used by our researchers.

MATLAB Simulink
Control System Toolbox Curve Fitting Toolbox
DSP System Toolbox Deep Learning Toolbox
Econometrics Toolbox Financial Toolbox
Fixed-Point Designer Fuzzy Logic Toolbox
MATLAB Compiler Optimization Toolbox
Parallel Computing Toolbox Partial Differential Equation Toolbox
Signal Processing Toolbox Simulink Control Design
Statistics and Machine Learning Toolbox Symbolic Math Toolbox
Wavelet Toolbox

Using MATLAB in HPC system

User should login any of the HPC Linux cluster machines first. Type the following command to set up the required environment variables for MATLAB:

System MATLAB version Module command
HPC2015 R2015a module load matlab/r2015a
R2015b module load matlab/r2015b
R2016b module load matlab/r2016b
R2017b module load matlab/r2017b
R2018b module load matlab/r2018b
HPC2021 R2021b module load matlab/r2021b

If version number is not provided (i.e. module load matlab), the latest MATLAB version will be used.

Interactive MATLAB

  • To start an interactive, command line (non GUI) version of MATLAB
    Please enter matlab -nodisplay from console.
  • To start an interactive GUI version of MATLAB
    If you are able to use X-11 forwarding and have enabled it in your SSH client software preferences, you can run MATLAB using the GUI by typing the command “matlab“. This will be similar to the one used on WINDOWS. (Reference: How to run a GUI application on HPC cluster systems? )

Only use interactive MATLAB session at hpc2015-file(X server of HPC2015 system). Since the master/login node of the HPC cluster system is a shared resource, running scripts that require significant computational resources will impact the usability of the cluster for others. Therefore you should not use interactive MATLAB sessions on the master node for any significant computation. If your MATLAB script requires significant time, CPU power, or memory, you should run your MATLAB program via the batch system.

Batch MATLAB Jobs

Batch job system is available in the HPC clusters only. In order to run MATLAB via the batch system, you will require a job submission script and a MATLAB M-file containing the script that will be run via the batch system. Sample programs and PBS/Slurm scripts can be obtained in /share1/matlab/sample/.

  1. Running serial MATLAB job
    Step 1: Prepare a “MATLAB ready” m-file. e.g. test.m

    disp 'Hello World'

    Step 2: Start MATLAB by entering command matlab -nodisplay

    Step 3: Compile the MATLAB m-file with mcc in MATLAB command prompt

    mcc -m test.m

    You will find an executable test in your current directory.

    Step 4: Prepare a job submission script. Sample PBS/SLURM script are as follows:
    For HPC2015 system – pbs-matlab.cmd

    #!/bin/sh
    #PBS –N test_matlab
    #PBS -q parallel
    #PBS -l walltime=00:10:00
    #PBS -l nodes=1:ppn=1
    #PBS -j oe
    
    module load matlab
    cd $PBS_O_WORKDIR
    ./test > ${PBS_JOBNAME}.${PBS_JOBID}
    # end of example file

    For HPC2021 system – slurm-matlab.cmd

    #!/bin/bash
    #SBATCH --job-name=t-matlab
    #SBATCH --time=30:00
    #SBATCH --cpus-per-task=32
    #SBATCH --nodes=1
    #SBATCH --ntasks=1
    #SBATCH --output=%x.out.%j 
    
    module load matlab
    cd ${SLURM_SUBMIT_DIR}
    ./test > ${SLURM_JOB_NAME}.${SLURM_JOBID}
    matlab -batch test.m
    

    Step 5: Submit the job with PBS/SLURM script.

    qsub pbs-matlab.cmd
    sbatch slurm-matlab.cmd
  2. Running multiple MATLAB serial jobs
    Step 1: Prepare a “MATLAB ready” m-file. e.g. test.m
    Step 2: Start MATLAB console mode by entering command matlab -nodisplay
    Step 3: Compile the MATLAB m-file with mcc in MATLAB command prompt

    mcc -m test.m

    You will find an executable “test” in your current directory.

    Step 4: Modify the file “msf.sh”.
    For example, change ./test > $1.log
    $1 means the serial job number, please do not alter it.

    Step 5: Modify the file “msf.dat” by changing the number of serial jobs

    ./msf.sh
    1
    1 10 1

    i.e. change “10” to “5”, meaning that you only require to run 5 serial jobs, and do not alter other parameters and format

    Step 6: Submit the job through pbs-msf.cmd
    You can alter the parameters according to our own usage

Additional Information

Official documentation is available at  MathWorks website.
Online tutorials and Webinars by Mathworks: