【问题标题】:Pass Parameters _ Shell Script - Octave Script传递参数 _ Shell 脚本 - Octave 脚本
【发布时间】:2011-01-18 13:44:11
【问题描述】:

如何将两个参数(数字向量)从 Shell 脚本传递到 Octave 脚本??

就是这样。

在“prove.sh”中

 #!/bin/bash

 .... do something that processing vector1 vector2 

./draw.m Vector1 Vector2

在“draw.m”中


 plot(Vector1, Vector2)

谢谢!!

【问题讨论】:

  • 您始终可以将数据向量写入文件,然后在 MATLAB/Octave 中读取它

标签: shell matlab command-line octave


【解决方案1】:

..而且,如果你允许的话,我会为 Octave 脚本添加一个小的变体,因为前者是在 Matlab 中的;)

Arrays.sh

#!/bin/bash
# create random data
for i in {1..10}; do v1[$i]=$RANDOM; done
for i in {1..10}; do v2[$i]=$RANDOM; done

# save data to file
echo ${v1[@]} > file.txt
echo ${v2[@]} >> file.txt

# call OCTAVE script
octave draw.m

Draw.m

load ("-ascii", "file.txt")
plot(file(1,:), file(2,:))      %# if you want see the graphic
print('figure.ps', '-deps')     %# save the result of 'plot' into a postscript file
exit

【讨论】:

    【解决方案2】:

    正如我在上面的 cmets 中提到的,您可以简单地将数据保存到文件中,然后调用 MATLAB/Octave 脚本,该脚本将依次从文件中加载数据。示例:

    arrays.sh

    #!/bin/bash
    
    # create random data
    v1=$(seq 1 10)
    for i in {1..10}; do v2[$i]=$RANDOM; done
    
    # save data to file
    echo $v1 > file.txt
    echo ${v2[@]} >> file.txt
    
    # call MATLAB script
    matlab -nodesktop -nojvm -nosplash -r "draw_data; quit" &
    

    draw_data.m

    load('/path/to/file.txt','-ascii')   %# load data
    plot(file(1,:), file(2,:), 'r')      %# plot
    saveas(gcf, 'fig.eps', 'psc2')       %# save figure as EPS file
    exit
    

    【讨论】:

      【解决方案3】:

      如果向量不是太长,您可以使用 --eval 选项在字符串中写入八度命令。

      prove.sh

      #!/bin/bash
      
      #  .... do something that processing vector1 vector2 
      vector1="[1 2 3 4 5 6 7 8 10]"
      vector2="[2 1 5 8 2 9 0 10 8]"
      
      # ..... using octave to plot and image witouth prompting
      octaveCommand="draw(${vector1},${vector2});"
      echo "Command to exec: ${octaveCommand}"
      octave -q --eval "${octaveCommand}"
      

      draw.m

      function draw(x,y)
          plot(x,y);
          print("graph.png","-dpng");
      

      -q 选项是为了避免在启动时出现 octave 消息。如果您不想关闭绘图窗口,您可以使用 --persist 选项来避免在命令完成后退出八度音程,但是您需要通过在终端中提示 exit 来手动结束它。至少它适用于 octave 3.2.3。要查看更多选项,您可以在终端中提示“octave --help”。

      【讨论】: