如何将两个参数(数字向量)从Shell脚本传递到八度脚本?
这就是想法..
#!/bin/bash
.... do something that processing vector1 vector2
./draw.m Vector1 Vector2
在“draw.m”中
plot(Vector1, Vector2)
谢谢!
答案 0 :(得分:2)
..而且,如果你允许我,我为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
答案 1 :(得分:1)
正如我在上面的评论中提到的,您可以简单地将数据保存到文件中,然后调用MATLAB / Octave脚本,该脚本将从文件中加载数据。例如:
#!/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" &
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
答案 2 :(得分:0)
如果vecotrs不太长,你可以使用--eval选项在字符串中写一个octave命令。
<强> 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选项是在启动时避免八度音程消息。如果您不希望关闭绘图窗口,则可以使用--persist选项在命令完成后避免八度音程退出但是您需要通过在终端中小费退出来手动结束它。至少它适用于八度音阶3.2.3。要查看更多选项,您可以在终端中提示“octave --help”。