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
How to call MATLAB script from command line?
提问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 matlab
coded 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 bash
script 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 matlab
like
没有论据。有几种方法可以解决这个问题,同时保持调用matlab
. 或者,您可以调用完整路径来matlab
喜欢
$ /Applications/MATLAB_R2015b.app/bin/matlab -nodesktop -nosplash -r "foo"
Setting Up matlab
Executable
设置matlab
可执行文件
Bash Script
Bash 脚本
Given that you have already written a bash
script to call matlab
the easiest solution is to alter it to include the $@
bash
wildcard 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 matlab
executable 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 matlab
bash
script in /usr/local/bin
and ensure that /usr/local/bin
is 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 PATH
with
我建议您将matlab
bash
脚本放入/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/bin
present. The bash
script 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 bash
script is to create a symbolic link to the matlab
executable. 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/bin
is in your PATH
.
同样对于这种方法,您需要确保它/usr/local/bin
在您的PATH
.
Adding matlab
to the PATH
添加matlab
到PATH
An alternative method is to simply add the directory where the matlab
executable resides to your PATH
. You can do this by modifying your .bash_profile
(or .bashrc
) file. Your .bash_profile
file resides in your home directory at ~/.bash_profile
. It is executed every time your user opens the Terminal. To add matlab
to the PATH
simply append
另一种方法是简单地将matlab
可执行文件所在的目录添加到您的PATH
. 您可以通过修改您的.bash_profile
(或.bashrc
)文件来做到这一点。您的.bash_profile
文件位于您的主目录中~/.bash_profile
。每次您的用户打开终端时都会执行它。添加matlab
到PATH
简单的追加
export PATH=$PATH:/Applications/MATLAB_R2015b.app/bin/
to it. Now you can call matlab
with
到它。现在你可以打电话matlab
给
$ matlab -nodesktop -nosplash -r "foo"
and this will locate the matlab
executable 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_profile
file 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 .bashrc
file instead of .bash_profile
because I use .bashrc
on Linux too. I have set my .bash_profile
file up to load my .bashrc
file
注意:我更喜欢修改.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 matlab
to 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/profile
file.
到系统范围的/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 matlab
to be available for every user and not just your user you need to modify the system-wide /etc/profile
file.
或重新启动终端以使更改生效。而且,如果您希望matlab
对每个用户可用,而不仅仅是对您的用户可用,则需要修改系统范围的/etc/profile
文件。
Executing matlab
from the Terminal
matlab
从终端执行
Now that we've set up matlab
to 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 cd
to the correct directory
我们可以看看实际执行的脚本。要执行一个 Matlab 脚本,我们首先需要在脚本所在的目录中,或者它可以在我们的 Matlab 中PATH
。我会假设它不在您的路径中,因此我们将cd
转到正确的目录
$ cd /path/to/foo.m
Now to execute matlab
without the desktop MathWorks tells us to use -nojvm -nodisplay -nosplash
but if we use -nojvm
and/or -nodisplay
we won't be able to display graphs. So we drop -nojvm
and replace -nodisplay
with -nodesktop
and use -nodesktop -nosplash
. This will launch Matlab without a display and allow us to display graphs. The correct command then to execute matlab
without 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 -r
option for the matlab
executable 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.m
becomes
或者,我们可以使用可执行文件的-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 中使用符号来重定向。