如何从 Linux 命令行调用 MATLAB 函数?

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

How to call MATLAB functions from the Linux command line?

linuxcommand-linematlab

提问by ablimit

Basically I have an mfile which looks like

基本上我有一个m文件看起来像

function Z=myfunc()
    % Do some calculations
    dlmwrite('result.out',Z,',');
end

I just want to execute it from the command line without getting into MATLAB. I tried several options (-nodisplay, -nodesktop, -nojvm, -r, etc.), none of them worked. I end up getting into MATLAB and have to type "quit" to exit.

我只想从命令行执行它而不进入 MATLAB。我试了几个选项(-nodisplay-nodesktop-nojvm-r,等),没有一次成功。我最终进入了 MATLAB 并且必须键入“quit”才能退出。

What is the solution?

解决办法是什么?

采纳答案by Alex Cohen

MATLABcan run scripts, but not functions from the command line. This is what I do:

MATLAB可以运行脚本,但不能从命令行运行函数。这就是我所做的:

File matlab_batcher.sh:

文件matlab_batcher.sh

#!/bin/sh

matlab_exec=matlab
X="()"
echo ${X} > matlab_command_.m
cat matlab_command_.m
${matlab_exec} -nojvm -nodisplay -nosplash < matlab_command_.m
rm matlab_command_.m

Call it by entering:

通过输入调用它:

./matlab_batcher.sh myfunction myinput

回答by Douglas Forman

Use:

用:

matlab -nosplash -nodesktop -logfile remoteAutocode.log -r matlabCommand

Make sure matlabCommandhas an exit as its last line.

确保matlabCommand将出口作为最后一行。

回答by Max

nohup matlab -nodisplay -nodesktop -nojvm -nosplash -r script.m > output &

回答by David Doria

You can call functions like this:

你可以这样调用函数:

matlab -r "yourFunction(0)"

matlab -r "你的函数(0)"

回答by quazgar

You could compile myfileinto a standalone program and run that instead. Use Matlab's compiler mccfor that (if you have it), more information is provided in this question.

你可以编译myfile成一个独立的程序并运行它。mcc为此使用 Matlab 的编译器(如果有的话),此问题中提供了更多信息。

This answer was copied from my answer to another question.

这个答案是从我对另一个问题的回答中复制的。

回答by gerrit

You can run an arbitrary function from the commandline by passing a command to Matlab, like this:

您可以通过向 Matlab 传递命令来从命令行运行任意函数,如下所示:

matlab -nodisplay -r "funcname arg1 arg2 arg3 argN"

This will execute the Matlab command funcname('arg1', 'arg2', 'arg3', 'argN'). Ergo, all arguments will be passed as strings and your function needs to handle this, but then again, this applies to command-line options in any other language as well.

这将执行 Matlab 命令funcname('arg1', 'arg2', 'arg3', 'argN')。因此,所有参数都将作为字符串传递,您的函数需要处理这个问题,但同样,这也适用于任何其他语言的命令行选项。

回答by Steven

I have modified Alex Cohen's answer for my own needs, so here it is.

我已经根据自己的需要修改了亚历克斯科恩的答案,所以在这里。

My requirements were that the batcher script could handle string and integer/double inputs, and that Matlab should run from the directory from which the batcher script was called.

我的要求是批处理脚本可以处理字符串和整数/双输入,并且 Matlab 应该从调用批处理脚本的目录中运行。

#!/bin/bash

matlab_exec=matlab

#Remove the first two arguments
i=0
for var in "$@"
do
 args[$i]=$var
 let i=$i+1
done
unset args[0]

#Construct the Matlab function call
X="("
for arg in ${args[*]} ; do
  #If the variable is not a number, enclose in quotes
  if ! [[ "$arg" =~ ^[0-9]+([.][0-9]+)?$ ]] ; then
    X="${X}'"$arg"',"
  else
    X="${X}"$arg","
  fi
done
X="${X%?}"
X="${X})"

echo The MATLAB function call is ${X}

#Call Matlab
echo "cd('`pwd`');${X}" > matlab_command.m
${matlab_exec} -nojvm -nodisplay -nosplash < matlab_command.m

#Remove the matlab function call
rm matlab_command.m

This script can be called like (if it is on your path): matlab_batcher.sh functionName stringArg1 stringArg2 1 2.0

这个脚本可以这样调用(如果它在你的路径上): matlab_batcher.sh functionName stringArg1 stringArg2 1 2.0

Where, the final two arguments will be passed as numbers and the first two as strings.

其中,最后两个参数将作为数字传递,前两个参数将作为字符串传递。

回答by DML

Here's a simple solution that I found.

这是我找到的一个简单的解决方案。

I have a function func(var)that I wanted to run from a shell script and pass it the first argument for var. I put this in my shell script:

我有一个函数func(var),我想从 shell 脚本运行它,并将它作为 var 的第一个参数传递。我把它放在我的 shell 脚本中:

matlab -nodesktop -nosplash -r "func('')"

That worked like a treat for me. The trick is that you have to use double quotes with the "-r" command for MATLAB and use single quotes in order to pass the bash argument to MATLAB.

这对我来说就像一种享受。诀窍是您必须在 MATLAB 的“-r”命令中使用双引号,并使用单引号将 bash 参数传递给 MATLAB。

Just make sure that the last line of your MATLAB script is "exit" or that you run

只需确保您的 MATLAB 脚本的最后一行是“退出”或您运行

matlab -nodesktop -nosplash -r "func(''); exit"