bash 如何从命令行调用 MATLAB 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33187141/
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-08 22:07:16  来源:igfitidea点击:

How to call MATLAB script from command line?

bashmatlab

提问by Breaking Bioinformatics

I have been trying to call matlab from Terminal (I have a Mac) and have it simply run a program for me, without display or anything, just the program. This program both displays graphs and writes a text file for me to use. I have found several of the previous answers on this question, and so I have tried:

我一直试图从终端(我有一台 Mac)调用 matlab 并让它为我简单地运行一个程序,没有显示或任何东西,只是程序。这个程序既显示图形,又写了一个文本文件供我使用。我在这个问题上找到了几个以前的答案,所以我尝试过:

   matlab -nodisplay -r foo 

   matlab -nodisplay -r foo.m 

   matlab -nojvm -nosplash -nodisplay -r foo;quit; 

   matlab -nojvm -nosplash -nodisplay -r "foo;quit;" 

   matlab -nojvm -nodesktop -r "foo;quit;" 

   matlab -nojvm -nodesktop -r
   "foo" 

   matlab -nojvm -nodesktop -nosplash -r foo

Pretty much every time I try it, I keep getting the same response (when the line just doesn't cause the prompt to say "screw you" and give up): the actual MATLAB program windowwill open, and it will stay open. It will interact normally. And command line won't do anything until I close out the window. The program I want doesn't run. The window just... sits there.

几乎每次我尝试它时,我都会得到相同的响应(当该行不会导致提示说“去你的”并放弃时):实际的 MATLAB 程序窗口将打开,并将保持打开状态。它会正常交互。在我关闭窗口之前,命令行不会执行任何操作。我想要的程序没有运行。窗户只是……坐在那里。

If it helps, this is how I have matlabcoded in my bin:

如果有帮助,这就是我matlab在 bin 中编码的方式:

#!/bin/bash                                                                                             

/Applications/MATLAB_R2015b.app/bin/matlab

I have no clue what is going on. Any help would be greatly appreciated.

我不知道发生了什么。任何帮助将不胜感激。

回答by IKavanagh

Your bashscript for calling Matlab will not pass any arguments to the Matlab executable. When you type

bash用于调用 Matlab 的脚本不会将任何参数传递给 Matlab 可执行文件。当你输入

$ matlab -nodesktop -nosplash -r "foo"

what is actually called is

实际上所谓的是

$ /Applications/MATLAB_R2015b.app/bin/matlab

without the arguments. There are several ways you can fix this whilst retaining the ease of just calling matlab. Alternatively you could call the full path to matlablike

没有论据。有几种方法可以解决这个问题,同时保持调用matlab. 或者,您可以调用完整路径来matlab喜欢

$ /Applications/MATLAB_R2015b.app/bin/matlab -nodesktop -nosplash -r "foo"

Setting Up matlabExecutable

设置matlab可执行文件

Bash Script

Bash 脚本

Given that you have already written a bashscript to call matlabthe easiest solution is to alter it to include the $@bashwildcard like

鉴于您已经编写了一个bash脚本来调用matlab最简单的解决方案是将其更改为包含$@bash通配符,例如

#!/bin/bash                                                                                             

/Applications/MATLAB_R2015b.app/bin/matlab "$@"

The $@wildcard passes all of the parameters you use, like -nodesktop -nosplash -r "foo"to the matlabexecutable so what is actually called now is

$@通配符通过了所有你使用,如参数-nodesktop -nosplash -r "foo"matlab什么是真正所谓现在是可执行文件等等

$ /Applications/MATLAB_R2015b.app/bin/matlab -nodesktop -nosplash -r "foo"

I recommend you place your matlabbashscript in /usr/local/binand ensure that /usr/local/binis in your PATH. The /usr/local/directory is for user installed scripts as opposed to system installed scripts. You can check what directories are in your PATHwith

我建议您将matlabbash脚本放入/usr/local/bin并确保它/usr/local/bin在您的PATH. 该/usr/local/目录用于用户安装的脚本,而不是系统安装的脚本。您可以检查在你的哪些目录PATH

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

and you should see an output similar to the above with /usr/local/binpresent. The bashscript should also be executable. You can set this with

并且您应该看到与上面类似的输出/usr/local/bin。该bash脚本也应该是可执行的。你可以用

$ sudo chmod +x /usr/local/bin/matlab

Note: OS X El Capitan places strong restrictions on where scripts can be installed via its new System Integrity Protectionfeature.

注意:OS X El Capitan 对可以通过其新的系统完整性保护功能安装脚本的位置施加了严格的限制。

Creating a Symlink to matlab

创建一个符号链接到 matlab

Another method similar to the creation of the bashscript is to create a symbolic link to the matlabexecutable. This again should be placed in /usr/local/bin

另一种类似于创建bash脚本的方法是创建指向matlab可执行文件的符号链接。这又应该放在/usr/local/bin

$ cd /usr/local/bin/
$ ln -s /Applications/MATLAB_R2015b.app/bin/matlab matlab

Also for this method you need to make sure that /usr/local/binis in your PATH.

同样对于这种方法,您需要确保它/usr/local/bin在您的PATH.

Adding matlabto the PATH

添加matlabPATH

An alternative method is to simply add the directory where the matlabexecutable resides to your PATH. You can do this by modifying your .bash_profile(or .bashrc) file. Your .bash_profilefile resides in your home directory at ~/.bash_profile. It is executed every time your user opens the Terminal. To add matlabto the PATHsimply append

另一种方法是简单地将matlab可执行文件所在的目录添加到您的PATH. 您可以通过修改您的.bash_profile(或.bashrc)文件来做到这一点。您的.bash_profile文件位于您的主目录中~/.bash_profile。每次您的用户打开终端时都会执行它。添加matlabPATH简单的追加

export PATH=$PATH:/Applications/MATLAB_R2015b.app/bin/

to it. Now you can call matlabwith

到它。现在你可以打电话matlab

$ matlab -nodesktop -nosplash -r "foo"

and this will locate the matlabexecutable in /Applications/MATLAB_R2015b.app/bin/and call it with

这将在其中找到matlab可执行文件/Applications/MATLAB_R2015b.app/bin/并调用它

$ /Applications/MATLAB_R2015b.app/bin/matlab -nodesktop -nosplash -r "foo"

After you modify your .bash_profilefile you need to reload it with

修改.bash_profile文件后,您需要重新加载它

$ source ~/.bash_profile

or restart the Terminal for the changes to take affect.

或重新启动终端以使更改生效。

Note: I prefer to modify the .bashrcfile instead of .bash_profilebecause I use .bashrcon Linux too. I have set my .bash_profilefile up to load my .bashrcfile

注意:我更喜欢修改.bashrc文件而不是.bash_profile因为我.bashrc也在 Linux 上使用。我已经设置了我的.bash_profile文件来加载我的.bashrc文件

$ cat .bash_profile 
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc

Note: If you want matlabto be available for every user and not just your user you need to add

注意:如果您希望matlab对每个用户都可用,而不仅仅是对您的用户可用,则需要添加

export PATH=$PATH:/Applications/MATLAB_R2015b.app/bin/

to the system-wide /etc/profilefile.

到系统范围的/etc/profile文件。

Creating an Alias for matlab

创建别名 matlab

The last method I'm going to mention is creating an alias for matlab. We do this by again modifying our .bash_profile(or .bashrc) file and appending

我要提到的最后一个方法是为matlab. 我们通过再次修改我们的.bash_profile(或.bashrc)文件并附加

alias matlab="/Applications/MATLAB_R2015b.app/bin/matlab"

to it. Again, after making changes we need to reload it with

到它。同样,在进行更改后,我们需要重新加载它

$ source ~/.bash_profile

or restart the Terminal for the changes to take affect. And, if you want matlabto be available for every user and not just your user you need to modify the system-wide /etc/profilefile.

或重新启动终端以使更改生效。而且,如果您希望matlab对每个用户可用,而不仅仅是对您的用户可用,则需要修改系统范围的/etc/profile文件。

Executing matlabfrom the Terminal

matlab从终端执行

Now that we've set up matlabto conveniently execute from the Terminal with the simple command

现在我们已经设置matlab为使用简单的命令从终端方便地执行

$ matlab

we can look at actually executing scripts. To execute a Matlab script we first need to be in the directory where the script resides or it could be in our Matlab PATH. I'll assume it is not in your path and so we'll cdto the correct directory

我们可以看看实际执行的脚本。要执行一个 Matlab 脚本,我们首先需要在脚本所在的目录中,或者它可以在我们的 Matlab 中PATH。我会假设它不在您的路径中,因此我们将cd转到正确的目录

$ cd /path/to/foo.m

Now to execute matlabwithout the desktop MathWorks tells us to use -nojvm -nodisplay -nosplashbut if we use -nojvmand/or -nodisplaywe won't be able to display graphs. So we drop -nojvmand replace -nodisplaywith -nodesktopand use -nodesktop -nosplash. This will launch Matlab without a display and allow us to display graphs. The correct command then to execute matlabwithout the full desktop GUI whilst also allowing us to display graphs is

现在在matlab没有桌面的情况下执行MathWorks 告诉我们使用-nojvm -nodisplay -nosplash但如果我们使用-nojvm和/或-nodisplay我们将无法显示图形。所以我们删除-nojvm并替换-nodisplay-nodesktop并使用-nodesktop -nosplash. 这将在没有显示的情况下启动 Matlab 并允许我们显示图形。然后在matlab没有完整桌面 GUI 的情况下执行同时还允许我们显示图形的正确命令是

$ matlab -nodesktop -nosplash

Now you can use the Terminal (command prompt) as the Matlab command window and execute commands as normal. For instance we could call foo

现在您可以使用终端(命令提示符)作为 Matlab 命令窗口并正常执行命令。例如,我们可以调用 foo

>> foo

Alternatively, we can use the -roption for the matlabexecutable to pass in commands for Matlab to execute. These must be quoted correctly and valid Matlab syntax. So our command to start Matlab with our previous options and to execute the script foo.mbecomes

或者,我们可以使用可执行文件的-r选项matlab来传递命令以供 Matlab 执行。这些必须正确引用并且有效的 Matlab 语法。所以我们用我们之前的选项启动 Matlab 并执行脚本的命令foo.m变成了

$ matlab -nodesktop -nosplash -r "foo"

Aside: If, for example, we were to use

旁白:例如,如果我们要使用

$ matlab -nodesktop -nosplash -r "foo; exit;"

(note the use of exit;) this would start Matlab, execute foo.m, display the graphs and then exit Matlab closing the graphs too.

(注意exit;)这将启动 Matlab,执行foo.m,显示图形,然后退出 Matlab 关闭图形。

回答by Yuval Atzmon

I think the bash script ignores the command line arguments. Could you try the following?

我认为 bash 脚本忽略了命令行参数。您可以尝试以下方法吗?

/Applications/MATLAB_R2015b.app/bin/matlab -nosplash -nodisplay -r "run foo.m;quit;"

回答by eyalsoreq

This should do the trick

这应该可以解决问题

matlab -nodisplay -nodesktop -nosplash -nojvm -r "foo($v1,$v2);exit"

enjoy

请享用

回答by alle_meije

You should be able to do

你应该能够做到

#!/bin/bash
/Applications/MATLAB_R2015b.app/bin/matlab < /path/to/foo.m

in other words use the <sign to re-direct, in Linux.

换句话说<,在 Linux 中使用符号来重定向。