linux系统下使用sh文件传参数给matlab程序

(1)编写sh文件
程序以下面的行开始(必须在文件的第一行):
   #!/bin/sh

定义需要传递的参数,用双引号引起,参数之间使用逗号或分号隔开

【编辑好脚本后执行需要修改权限:chmod +x filename.sh,filename是sh文件的名字】

(2)举例:sh文件传递文件夹路径给matlab程序,matlab读取图片后将其转为灰度图存储。

路径‘/home/sjxy/hello/image/’下的图片。
hello.sh文件:
#!/bin/bash
imagepath='/home/sjxy/hello/image/'
/usr/local/MATLAB/R2014b/bin/matlab -nodesktop -nosplash -r "impath='$imagepath'",</home/sjxy/hello/imageread.m> /home/sjxy/hello/bb.out &
需要传递的图片路径为imagepath,matlab中使用impath接收该路径
修改hello.sh文件权限:chmod +x hello.sh
【/usr/local/MATLAB/R2014b/bin/matlab 是matlab的路径】
【不启动图形界面运行matlab:matlab -nodesktop -nosplash】
【在命令行直接运行matlab需要使用-r选项:matlab -nodesktop -nosplash -r </path/filename.m> /path/bb.out &】   
   Matlab程序:                       
I=imread(fullfile(impath,'1.jpg'));  %-- load the image
G=rgb2gray(I);
imwrite(G,[impath,'gray.jpg']); %-- save gray image
运行hello.sh: ./hello.sh       [./表示在当前目录下查找文件]
结果:
生成bb.out文件,指定路径下存储了灰度图。

传递多个参数:

sh文件:

#!/bin/bash

imagepath='/home/sjxy/hello/image/'

imagepath2='/home/sjxy/hello/im/'

 

/usr/local/MATLAB/R2014b/bin/matlab-nodesktop -nosplash -r  "impath='$imagepath',impath2='$imagepath2'",</home/sjxy/hello/imageread.m> /home/sjxy/hello/bb.out &
(或"impath='$imagepath';impath2='$imagepath2'")
matlab程序: