Filter löschen
Filter löschen

Passing a filename from a shell script to Matlab

4 Ansichten (letzte 30 Tage)
saipb
saipb am 8 Sep. 2019
Kommentiert: saipb am 8 Sep. 2019
Hello,
I am trying to pass a filename from my shell script to my Matlab script.
!/bin/bash
filename='subset_wrfout.nc'
echo $filename
module load matlab
#*************************************************************************
cat > test.m << EOF
fname = getenv('filename')
dummy = ones(1,2);
f = strcat(fname,'.mat');
save(f,'dummy');
EOF
#*************************************************************************
matlab -nojvm -nodisplay -nosplash -r "run('test.m'); exit;"
I tried using getenv. However, the result is always a null array or some root directory. The command is just not picking on the environmental variable that I created in shell. Why might this be this case? Note that I need the filename in the start as well as in the end while I am using it store the variable.
  3 Kommentare
saipb
saipb am 8 Sep. 2019
Thank you for the prompt response. Do you mean in the Shell script or in the Matlab script? I tried using just filename='subset_wrfout.nc' as well as export filename='subset_wrfout.nc' in the shell script to no effect. Is there anything I should change in the Matlab script?
saipb
saipb am 8 Sep. 2019
I solved this issue.
Even if I do export filename='subset_wrfout.nc' in the shell script, when I get into the Matlab script and say,
fname = getenv('filename');
disp(fname);
It only gives me
fname =
'/share/software/user/restricted/matlab/R2019a/bin/matlab'
I figured that getenv might be the wrong command for this, so I resorted to simple echo and system.
[status,cmdout] = system('echo $filename');
fname = cmdout;
disp(fname)
fwithoutnc = erase(fname,".nc")
dummy = ones(1,2);
f = strcat(fwithoutnc,'.mat');
disp(f)
save(f,'dummy');
Output:
subset_wrfout.nc
fwithoutnc =
'subset_wrfout'
subset_wrfout.mat
Thank you!!

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by