kafka
kafka

Reputation: 949

Pass Parameters _ Shell Script - Octave Script

How can i pass two parameters (number vectors) from a Shell Script to a Octave Script ??

That's the idea..

In "prove.sh"

 #!/bin/bash

 .... do something that processing vector1 vector2 

./draw.m Vector1 Vector2

In "draw.m"


 plot(Vector1, Vector2)

Thank you!!

Upvotes: 3

Views: 4126

Answers (3)

user688877
user688877

Reputation: 444

If the vecotrs are not too long you can use the --eval options to write an octave command in a string.

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");

The -q option is to avoid the octave message at the startup. If you don't want the plot window to close you can use the --persist option to avoid octave to exit once the command is done but then you will need to manually end it by tipping exit in the terminal. At least it works for octave 3.2.3. To see more options you can tip "octave --help" in a terminal.

Upvotes: 0

Zany
Zany

Reputation: 328

..And, if you allow me, i add a small variation for a Octave Script since the former was in 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

Upvotes: 4

Amro
Amro

Reputation: 124553

As I mentioned in the comments above, you can simply save the data to a file then call the MATLAB/Octave script which will in turn load the data from file. Example:

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

Upvotes: 1

Related Questions