필터 지우기
필터 지우기

Passing a filename from a shell script to Matlab

조회 수: 4 (최근 30일)
saipb
saipb 2019년 9월 8일
댓글: saipb 2019년 9월 8일
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
saipb
saipb 2019년 9월 8일
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 2019년 9월 8일
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!!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by