matlab和linux 传参数吗,linux系统使用sh文件传参数给matlab程序

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

(1)编写sh文件

程序以下面的行开始(必须在文件的第一行):

#!/bin/sh

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

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

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

095ef6ece56929294924d3f588124319.png

路径‘/home/sjxy/hello/image/’下的图片。

4b20d586c987d7de0423b415b103a4dc.png

hello.sh文件:

#!/bin/bash

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

/usr/local/MATLAB/R2014b/bin/matlab -nodesktop -nosplash -r "impath='$imagepath'", /home/sjxy/hello/bb.out &

db1684bdab56e0c0305eb48b87794075.png

需要传递的图片路径为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/bb.out &】

Matlab程序:

I=imread(fullfile(impath,'1.jpg'));  %-- load the image

G=rgb2gray(I);

imwrite(G,[impath,'gray.jpg']); %-- save gray image

5208a5a944804435c880db181342423a.png

运行hello.sh: ./hello.sh       [./表示在当前目录下查找文件]

542b4af18c4baec1175b22dc6c9dd50b.png

结果:

生成bb.out文件,指定路径下存储了灰度图。

d83f347f94d9b9ad90301a543646e84a.png

91afd2dd7f3cd385dfa8f56b40469d10.png

传递多个参数:

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/bb.out &

492727dbc5887cdd89259f54b26b1619.png

(或"impath='$imagepath';impath2='$imagepath2'")

matlab程序:

4072a8797c17d98406a76750c8e60357.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值