Tuesday, March 15, 2022

[SOLVED] Setting and passing string variable in SLURM job script

Issue

I have a SLURM job script as follows:

#!/bin/bash


#SBATCH -o ./out/%x.%j.%N.out
#SBATCH -e ./out/%x.%j.%N.err
#SBATCH -D ./
#SBATCH -J matlab_serial_batch_job
#SBATCH --nodes=1
#SBATCH --tasks-per-node=1
#SBATCH --cpus-per-task=1
#SBATCH --time=0:30:00
 

module load slurm_setup
module load matlab/R2021a-generic
 

NROWA=1000
NCOLA=2000
NROWB=2000
NCOLB=5000
MYSTRING='blablabla'
 

# Run MATLAB
matlab -nodisplay -singleCompThread \
       -r "matmul_serial([$NROWA $NCOLA], [$NROWB $NCOLB], $MYSTRING);"

As you might see, I would like to set and pass a string variable called MYSTRING to the MATLAB function matmul_serial. However, I got the following error:

Unrecognized function or variable 'blablabla'

For those integer variables NROWA, NROWB, NCOLA and NCOLB, they work pretty fine. How do I properly set and pass the string variable MYSTRING in this context?


Solution

Actually, the single quotes will be striped by Bash during the assignment.

❯ MYSTRING='blablabla'
❯ echo $MYSTRING
blablabla

You can try to escape the quotes like this:

❯ MYSTRING=\'blablabla\'
❯ echo $MYSTRING
'blablabla'

Otherwise, Matlab wrongly believe blablabla is the name of a variable or a function and not a string literal. The error message is misleading though as it adds a single quote around blablabla. But those single quotes are not the ones that you inserted in the MYSTRING=... assignment, as shown here below in an interactive Matlab session.

$ matlab
MATLAB is selecting SOFTWARE OPENGL rendering.

                                                                            < M A T L A B (R) >
                                                                  Copyright 1984-2019 The MathWorks, Inc.
                                                                  R2019a (9.6.0.1072779) 64-bit (glnxa64)
                                                                               March 8, 2019


To get started, type doc.
For product information, visit www.mathworks.com.

>> blablabla
Undefined function or variable 'blablabla'.



Answered By - damienfrancois
Answer Checked By - Robin (WPSolving Admin)