Search Docs by Keyword

Table of Contents

MATLAB Figures in a Batch Mode

This web-page illustrates how to create MATLAB figures in a batch-mode (without a GUI) on the cluster. This is especially useful if you would like to do your computations and post-processing all together in a consistent computing environment. Below is an example function which generates data, creates a figure, and saves it as a file on the cluster:

%=========================================================================================
% Program: print_figure.m
%
% Usage:
% matlab -nodesktop -nodisplay -nosplash -r “print_figure(‘file_name’,’file_format’);exit”
%=========================================================================================
function [] = print_figure( outfile, file_format )
disp(‘A simple test to illustrate generating figures in a batch mode.’);
x = 0:.1:1;
A = exp(x);
plot(x,A);
print(outfile,file_format);
end

The specific example saves the figure as a PNG (24-bit) image. For complete list of available image formats, please refer to the official MATLAB documentation.
Here is an example SLURM batch-job submission script for sending the job to the queue:

#!/bin/bash
#SBATCH -J print_figure
#SBATCH -o print_figure.out
#SBATCH -e print_figure.err
#SBATCH -p serial_requeue
#SBATCH -n 1
#SBATCH -t 30
#SBATCH –mem=2000
matlab -nodesktop -nodisplay -nosplash -r “print_figure(‘out’,’-dpng’);exit”

Please, notice the options -nodesktop -nodisplay -nosplash on the MATLAB command line. These reassure the figure is generated properly without the GUI. If you name this script, e.g., print_figure.run, it is submitted to the queue with

sbatch print_figure.run

Upon job completion the file “out.png” is generated in the work directory.

© The President and Fellows of Harvard College
Except where otherwise noted, this content is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.