Dartmouth Research Computing & Data (RCD) logo
  • Home 
  • HPC 
  • AI 
  • Services 
  • Status 
  • About Us 

  •   Search this site
  •  
  1.   MATLAB
  1. Home
  2. Research Software
  3. MATLAB
  4. MATLAB - Batch Script Example

MATLAB - Batch Script Example

Overview  

The following example involves submitting a MATLAB script or function to a job scheduler (like SLURM or PBS) for execution in the Discovery high performance computing environment.

Example  

This example assumes the user has a Research Computing account and is logged in to the Discovery high performance computing environment

Matlab “.m” files with HPC batch scripts (sbatch shell scripts)

Sample “.m” file, test-m-file.m:

disp("show this text")
Pitch = [0.7;0.8;1;1.25;1.5];
Shape = {'Pan';'Round';'Button';'Pan';'Round'};
Price = [10.0;13.59;10.50;12.00;16.69];
Stock = [376;502;465;1091;562];
T = table(Pitch,Shape,Price,Stock)
writetable(T,'tabledata.txt');

Sample batch file to submit the “.m” file above, sbatch_run_mfile.sh:

#!/bin/bash -l
# Request 1 CPU for the job
#SBATCH --cpus-per-task=1
# Request 8GB of memory for the job
#SBATCH --mem=8GB
# Walltime (job duration)
#SBATCH --time=00:05:00
# Then finally, our code we want to execute.
module load matlab
matlab -nodisplay -nodesktop -nosplash -nojvm -r "run test-m-file.m;quit"

Run the sbatch shell script:

$ sbatch batch_matlab_m4.sh

View file in queue:

$ squeue -u f002d69
             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
           3024150  standard batch_ma GaughanS  R       0:02      1 q01

When the batch job finishes, view the batch file’s “.out” file:

$ less slurm-3024090.out

View the data written to the output file:

$ less tabledata.txt

Reference (Matlab support): https://www.mathworks.com/help/matlab/import_export/write-to-delimited-data-files.html

 MATLAB - Overview and General Resources
Copyright © 2025 Dartmouth Research Computing & Data | Powered by Hinode.
Dartmouth Research Computing & Data (RCD)
Code copied to clipboard