matlab - 如何: MATLAB script that can get arguments from Unix command-line

标签 matlab command-line command-line-arguments

只是为了具体起见,考虑以下 super 简单的 Python 脚本,在一个名为 add_em 的文件中:

#!/usr/bin/env python
# script name: add_em

from sys import argv

x = int(argv[1])
y = int(argv[2])
x_plus_y = x + y
print '%d' % x_plus_y

我现在可以从 Unix 命令行运行此脚本,并向其传递参数,如下所示:

% python add_em 3 8
11

如果我使脚本可执行,我什至不需要在命令行中提及 python:

% chmod +x add_em
% add_em 41 -29
12

有人可以告诉我如何编写(和运行)MATLAB 脚本,使其与上面的脚本完全一样吗?特别是,它必须能够从 Unix 命令行(相对于 MATLAB GUI 的“命令行”)读取其参数,并将它们的数字总和打印到标准输出。

NOTE: this script need not be "stand-alone"; IOW, one may assume that MATLAB is locally installed, and even that one can mention matlab in the command line (analogously to the first form above, where the python interpreter is explicitly invoked on the command line).

谢谢!

PS:不用说,这个脚本是“健壮”的对立面,但我的目的是生成一个易于传达的示例。

最佳答案

您可以在 add_em.m

中使用 MATLAB 函数执行您想要的操作
function add_em(x, y)
x_plus_y = x + y;
disp(x_plus_y);
exit;

然后使用 -r 开关从 Unix 命令行调用它。示例:

matlab -nodesktop -nojvm -nosplash -r "add_em(3, 8)" 

- 选项抑制桌面、java 和启动,因此脚本/函数将在没有额外开销的情况下执行。

您还可以通过将输出重定向到日志文件(用于任何计算)或拖尾来另外抑制 MATLAB 欢迎/版权消息,例如,为了在终端上打印一些内容

matlab -nosplash -nodesktop -nojvm -r "add_em(3, 8)" | tail -n 3

更新:刚找到这篇文章/答案的相关信息:suppress start message of Matlab

关于matlab - 如何: MATLAB script that can get arguments from Unix command-line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12223179/

相关文章:

linux - 如何通过名称列表复制文本文件的某些行

windows - cmd 上的 Matlab (winxp)

matlab - Matlab 图形中的半透明标记

algorithm - 从两个 3D 多集数组中找到任意两个相应多集的交集大小的更快方法

matlab - 我可以在 Matlab 中快速执行以下操作吗?

objective-c - 使用 NSWorkspace 获取已启动应用程序的进程 ID

由元组索引的 Python 列表

python - 将 IPython 变量作为参数传递给 bash 命令

c++ - 在 Windows 中使用 boost::program_options 从命令行参数读取 Unicode 字符

parameter-passing - 将 arg 添加到启动文件