Share:

Matlab

Matlab execution

In order to execute a Matlab simulation, you should execute:

>> srun -p <partition> -c <number of CPUs> --mem <RAM size in MB> matlab -batch "<command>;exit;"&

We recommend the usage of byobu or tmux in order to assure the process continuity.

How to print the results in the command line

Let’s put an example. We want to execute the following matlabtest.m file:

function test = matlabtest ()
A = 1;
B = 1;
C = A+B;
end

>> srun -p gpi.compute -c 1 --mem 1000 /opt/matlab2022a/bin/matlab -batch matlabtest

If we run the above command, the execution will work, but anything will be print on the command line.

To print the results, we can use the disp() function. More info here.

function test = matlabtest ()
A = 1;
B = 1;
C = A+B;
disp('Result:')
disp(C);
end

If what we want is to store all the output in a file, we can use the diary function. More info here

function test = matlabtest ()
diary  on;
diary log.txt;
A = 1;
B = 1;
C = A+B;
disp('Result:')
disp(C);
end

However, through the command line, we can not show the graphic results. To be able to get the graphics, we must save these figures with the savefig() function. More info here

function test = matlabtest ()
diary  on;
diary log.txt;
figure=bar([1 11 7 8 2 2 9 3 6]);
savefig('grafic.fig');
end

Moreover, we can use sbatch instead of srun, to execute a bash script. Here you can read the sbatch tutorial.

Example of myscript.sh:

#!/bin/bash
#SBATCH -p gpi.compute          # Partition to submit to
#SBATCH --mem=1G                  # Max CPU Memory
#SBATCH --gres=gpu:1
matlab -batch matlabtest

>> sbatch myscript.sh