I am using multiple text files to set variables and array variables. These are the input arguments to a MATLAB function, that iteratively creates url download strings based on these variables, and then downloads temperature data using MATLAB's websave function. Notably, the MATLAB script uses parfor to parallelise a for loop and spread it over 44 cores on a computing cluster. The MATLAB runs on my laptop, but extremely slowly, that's why I need to run it from the High Performance Cluster.


I am having issues in passing these variables set in the bash script, to the matlab function. And I am left on the MATLAB command line in the end. Note I have tried removing the single quotes from around the variables in the matlab function call, however I get the same error.


cd CRW_Temp_Profiles

# Set Variables
IFS=$'\r\n' GLOBIGNORE='*' command eval  'LatArray=($(cat Lat.txt))'
IFS=$'\r\n' GLOBIGNORE='*' command eval  'LongArray=($(cat Long.txt))'
IFS=$'\r\n' GLOBIGNORE='*' command eval  'SitesArray=($(cat Sites.txt))'
Site=$(echo ${SitesArray[${SLURM_ARRAY_TASK_ID}-1]})

# Check Variables were read properly
echo 'Site is Number '${Site}
echo 'Row-specific LatLons are:'
echo '    deg N = '${LatArray[${SLURM_ARRAY_TASK_ID}-1]}
echo '    deg S = '${LongArray[${SLURM_ARRAY_TASK_ID}-1]}
echo ' All Lats are'
echo ${LatArray[*]}

# run MATLAB function
module load MATLAB
matlab -nodisplay -nojvm -nodesktop -nosplash -r "try; f_Download_CRW_Temp('$LatArray', '$LongArray', "1985", "2019", '$Site', "44", ".csv"); end; quit;"
module purge

Output and Error Message

Site is Number 1
Row-specific LatLons are:
    deg N = -16.5
    deg S = 149.8
 All Lats are
-16.5 -20 -19.5 -11.5 -23.5 -21.5 -31.5 ..... <100 entries of site latitudes> .... 11.2 11.2

                                                                                          < M A T L A B (R) >
                                                                                Copyright 1984-2018 The MathWorks, Inc.
                                                                                R2018b ( 64-bit (glnxa64)
                                                                                            August 28, 2018

For online documentation, see https://www.mathworks.com/support
For product information, visit www.mathworks.com.

 try; f_Download_CRW_Temp('-16.5', '149.8', 1985, 2019, '1', 44, .csv); end; quit;
Error: Invalid use of operator.

I found the solution. When you are putting string variables in the matlab function as arguments, in matlab they would appear in double quotes "", but in the 1-line script the entire matlab call is in double quotes, so you must also escape the argument-specifc double quotes with "\". So the code looks like: matlab -nodisplay -nojvm -nodesktop -nosplash -r "f_Download_CRW_Temp('$LatArray', '$LongArray', \"1985\", \"2019\", '$Site', \"44\", ".csv"); quit;"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.