shell传参给matlab问题解决办法

之前需要通过shell脚本传参给matlab程序,但是遇到一些问题,现将我遇到的问题分享出来,给遇到同样问题的人一些借鉴。

shell部分脚本Execl.sh:

/usr/bin/python /home/shk/IPMProduct.py $1

cd /home/shk/ && /usr/local/MATLAB/R2015b/bin/matlab -nodesktop -nosplash -r "sDate='$1';test;quit"

ps:

1./usr/bin/python /home/shk/IPMProduct.py $1。为调用Python程序,IPMProduct.py为python程序名,$1为shell脚本接收的第一个参数

2.cd /home/shk/ && /usr/local/MATLAB/R2015b/bin/matlab -nodesktop -nosplash -r "sDate='$1';test;quit"。为调用matlab程序,test为matlab程序名,sDate='$1'为传入matlab的字符型参数,matlab中用同样的变量sDate接收。quit为执行完test程序后退出matlab环境。-nodesktop -nosplash -r这几个参数见上一篇文档说明。

matlab程序部分代码 test.m:直接使用sDate变量即可

%clc;

%close all;

%clear;

filedir=['/gds/',sDate,'/'];

filenameT = dir(fullfile(filedir,'*_030KM_MS_UDS.HDF'));

filenameArr = char({filenameT.name})

fileoutdir=['/gds/DATA/',sDate,'/'];

[fileoutnameT]=mapplot(filedir,filenameArr,F107,fileoutdir);

题外话:之前由于matlab中clc; close all;clear;没有注释掉,每次传进来的参数值都被清理掉了,sDate没有获取数值,导致MATLAB程序错误。注释掉之后程序正常运行。真的是,这个问题困扰了我好久,一直以为是shell脚本中传参给matlab的程序没写对,查了好多资料,后来才发现是因为进入matla后参数都被清理掉了。