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

本文介绍如何在Linux系统中通过shell脚本传递参数给Matlab程序,并演示了具体的实例,如传递文件夹路径给Matlab进行图片处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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程序:
希望可以帮助到大家大笑比心





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值