在shell脚本中运行MATLAB函数(Mac)

时间:2017-06-02 18:48:03

标签: bash matlab shell terminal

有没有办法在shell脚本中运行带有输入的MATLAB函数,然后打印/存储输出?我写了一个示例函数和shell脚本,运行没有错误,但没有提供任何结果。

我希望从shell脚本中运行函数 sample.m (位于名为' cluster'的文件夹中),输入 n m ,然后将结果分配给工作区。我的最终目标是向集群提交更复杂的代码,但我想先了解shell脚本如何更好地工作。

我想用生成它们的 n m 的值记录每次迭代的输出。有一个简单的方法吗?

这是我的样本MATLAB函数:

 function x = sample(n,m)
    x = 10*rand(n,m);
 end

这是我的shell脚本:

#!/bin/bash 
for m in 1 2 3 4
do 
for n in 1 2 3 4
do
     cmd=$ /Applications/MATLAB_R2016b.app/bin/matlab -nodesktop -nojvm -nodisplay -nosplash -r "try; sample $n $m; end; quit;" 
     echo $cmd
done
done

这是终端的结果:

ssss:cluster cbruno$ bash sample.sh

                            < M A T L A B (R) >
                  Copyright 1984-2016 The MathWorks, Inc.
                   R2016b (9.1.0.441655) 64-bit (maci64)
                             September 7, 2016


For online documentation, see http://www.mathworks.com/support
For product information, visit www.mathworks.com.


ssss:cluster cbruno$ 

1 个答案:

答案 0 :(得分:0)

请使用“-logfile”选项,如下所示

#!/bin/bash 
for m in 1 2 3 4
do 
for n in 1 2 3 4
do
    cmd=$ /Applications/MATLAB_R2016b.app/bin/matlab -nodesktop -nojvm -nodisplay -nosplash -logfile log-$m-$n.txt -r "try; sample $n $m; end; quit;" 
    echo $cmd
done
done