编写 bash 脚本以在 UNIX 中运行 matlab 程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17894676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:02:06  来源:igfitidea点击:

Writing a bash script to run a matlab program in UNIX?

bashmatlabshellunix

提问by Shaayaan Sayed

I have no bash scripting background. I know the basics of unix command line.

我没有 bash 脚本背景。我知道unix命令行的基础知识。

I have a matlab script that takes in an matrix image as input. I have a bunch of images in a file. I need the bash script to call the matlab program and feed every single image in the file as input to the program one by one.

我有一个将矩阵图像作为输入的 matlab 脚本。我在一个文件中有一堆图像。我需要 bash 脚本来调用 matlab 程序并将文件中的每个图像作为程序的输入一一输入。

If you want to write it go ahead that would be great. All I'm asking is for someone to direct me to exactly what I should look into.

如果你想写它,那就太好了。我所要求的只是有人指导我确切地了解我应该研究的内容。

Thanks

谢谢

回答by SheetJS

Assume that the matlabcommand is in your path (for more info, See the documentation)

假设该matlab命令在您的路径中(有关更多信息,请参阅文档

Supposing you just wanted to get all of the BMP's (you didn't specify file type), you can use a simple bash loop:

假设您只想获取所有 BMP(您没有指定文件类型),您可以使用一个简单的 bash 循环:

for f in *.BMP; do
    matlab -nosplash -nodisplay -nojvm -r "my_command('$f'),quit()"
done

The -nosplash, -nodisplay, and -nojvmensure that the GUI isn't triggered, and the -rinstructs matlab to run the command. The ,quit()at the end ensures that matlab quits after running.

-nosplash-nodisplay以及-nojvm确保GUI不会被触发,并-r指示MATLAB来运行该命令。在,quit()运行后,在年底确保MATLAB退出。

回答by jm666

You need

你需要

  • one command what produces list of images one per line. Call it as list_images, or a filenamewhat contains the list of images
  • one cycle
  • command, what reads the images
  • one variable (calling it "image_name")
  • comamnd to run matlab
  • place, here the output should go
  • 一个命令产生每行一个图像列表。将其称为list_images,或filename包含图像列表的内容
  • 一个周期
  • 命令,读取图像的内容
  • 一个变量(称为“image_name”)
  • 命令运行matlab
  • 地方,这里的输出应该去

Something like:

就像是:

list_images | while read image_name
do
     matlab_command "$image_name" > "$image_name.output"
done

or better

或更好

while read image_name
do
    matlab_command "$image_name" > "$image_name.output"
done < filename_with_images.txt

Not really understand what you mean with the have amatrix images in a file, post an example of your input.

不太明白您对 的意思have amatrix images in a file,请发布您输入的示例。

EDITfor finding prjfiles in the curent directory

编辑prj在当前目录中查找文件

find . -maxdepth 0 -name \*.prj -print0 | while IFS= read -r -d '' image_name
do
    matlab_command "$image_name" > "$image_name.output"
done

Google for "bash tutorial".

谷歌搜索“bash 教程”。