简体   繁体   English

matlab 从命令行 linux 行执行脚本

[英]matlab execute script from command linux line

is there a way to run a matlab script from linux command line?有没有办法从 linux 命令行运行 matlab 脚本? For instance, I have the following simple script "test.m":例如,我有以下简单的脚本“test.m”:

x = [1,2,3];
y = [2;3;4];

prod = x*y
disp(prod)

So what I want is to be able to execute that script from the linux command line without opening the matlab GUI or the matlab command line.所以我想要的是能够在不打开 matlab GUI 或 matlab 命令行的情况下从 linux 命令行执行该脚本。 That is, I expect something like that:也就是说,我期待这样的事情:

~$ matlab test.m

and I expect to see the result of the product on the linux command line.我希望在 linux 命令行上看到产品的结果。

I know that you can do that with python eg,我知道你可以用 python 做到这一点,例如,

~$ python test.py

but was wondering if the same can be achieved with matlab.但想知道是否可以用 matlab 实现同样的效果。

In order to run a script you can open Matlab (you can prevent run it without the GUI using -nodisplay and -nodesktop flags), then run the script using the run command, and finally close matlab using exit .为了运行脚本,您可以打开 Matlab(您可以使用-nodisplay-nodesktop标志阻止在没有 GUI 的情况下运行它),然后使用run命令运行脚本,最后使用exit关闭 matlab。

You can do all this from a terminal with a single instruction:您可以通过一条指令从终端完成所有这些操作:

matlab -nodisplay -nosplash -nodesktop -r "run('path/to/your/script.m');exit;"

However Matlab outputs the welcome message to the console before running your script.但是,在运行脚本之前,Matlab 会将欢迎消息输出到控制台。 To get rid of the welcome message just skip the first 11 lines (10 depending on your Matlab version) using tail -n +11要摆脱欢迎消息,只需使用tail -n +11跳过前 11 行(10 行取决于您的 Matlab 版本)

So your final instruction will be:所以你的最终指令是:

matlab -nodisplay -nosplash -nodesktop -r "run('path/to/your/script.m');exit;" | tail -n +11

Starting with R2019a, the preferred method would be, for your test.m script:从 R2019a 开始,首选方法是 test.m 脚本:

matlab -batch "test"

This has several advantages, mainly no need for all the -no flags and MATLAB will exit with non-zero status if test.m (must be on search path) contains an error.这有几个优点,主要是不需要所有-no标志,如果test.m (必须在搜索路径上)包含错误,MATLAB 将以非零状态退出。

From the documentation page, matlab (Linux) :从文档页面, matlab (Linux)

Execute MATLAB script, statement, or function non-interactively.以非交互方式执行 MATLAB 脚本、语句或函数。 MATLAB: MATLAB:

  • Starts without the desktop在没有桌面的情况下启动
  • Does not display the splash screen不显示启动画面
  • Executes statement执行语句
  • Disables changes to preferences禁用对首选项的更改
  • Disables toolbox caching禁用工具箱缓存
  • Logs text to stdout and stderr将文本记录到stdoutstderr
  • Does not display dialog boxes不显示对话框
  • Exits automatically with exit code 0 if script executes successfully.如果脚本成功执行,则自动退出,退出代码为 0。 Otherwise, MATLAB terminates with a non-zero exit code.否则,MATLAB 将以非零退出代码终止。

statement is MATLAB code enclosed in double quotation marks.语句是用双引号括起来的 MATLAB 代码。 If statement is the name of a MATLAB function or script, do not specify the file extension.如果statement是 MATLAB 函数或脚本的名称,则不要指定文件扩展名。 Any required file must be on the MATLAB search path or in the startup folder.任何必需的文件都必须位于 MATLAB 搜索路径或启动文件夹中。

Use the -batch option in non-interactive scripting or command line work flows.在非交互式脚本或命令行工作流中使用-batch选项。 Do not use this option with the -r option.不要将此选项与-r选项一起使用。

To test if a session of MATLAB is running in batch mode, call the batchStartupOptionUsed function.要测试 MATLAB 会话是否以批处理模式运行,请调用batchStartupOptionUsed函数。

Example: -batch "myscript"示例: -batch "myscript"

I created a basic shell script called runm and put in my path:我创建了一个名为runm的基本 shell 脚本并将其放入我的路径中:

$ runm mymatlab.m

Script:脚本:

# simple script to run matlab script
if [ $# -eq 0 ]
  then
    echo "please pass m script"
fi

matlab -nodisplay -nosplash -nodesktop -r "run('$1');"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM