フィルターのクリア

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 Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by