0

given is a function f(a,b,x,y) in gnuplot, where we got a 3D-space with x,y,z (using splot).

Also given is a csv file (without any header) of the following structure:

2  4
1  9
6  7
... 

Is there a way to read out all the values of the first column and assign them to the variable a? Implicitly it should create something like:

a = [2,1,6]
b = [4,9,7]

The idea is to plot the function f(a,b,x,y) having iterated for all a,b tuples.

I've read through other posts where I hoped it would be related to it such as e.g. Reading dataset value into a gnuplot variable (start of X series). However I could not make any progres.

Is there a way to go through all rows of a csv file with two columns, using the each column value of a row as the parameter of a function?

Community
  • 1
  • 1
Daniyal
  • 885
  • 3
  • 16
  • 28
  • 1
    I don't understand what you're trying to do: do you want to use all the possible [a,b] combinations to *evaluate* a function f(a,b,x,y) whose functional dependence on a, b, x and y you already know or do you want to *parametrize* said function somehow using the a,b values loaded from file? – Miguel Oct 25 '15 at 11:21
  • 1
    The last point. And thank you for your solutions. It was exactly what I've been looking for. Special thanks also for providing a gnuplot only variant. It took some time to comprehend it, but now I finally got it how it works. :-) @Brian Tompsett, thank you for editing! :-) – Daniyal Oct 25 '15 at 15:08
  • 1
    See also [Plotting a function directly from a text file](http://stackoverflow.com/q/15007620/2604213) – Christoph Oct 25 '15 at 16:55

1 Answers1

1

Say you have the following data file called data:

1 4
2 5
3 6

You can load the 1st and 2nd column values to variables a and b easily using an awk system call (you can also do this using plot preprocessing with gnuplot but it's more complicated that way):

a=system("awk '{print $1}' data")
b=system("awk '{print $2}' data")
f(a,b,x,y)=a*x+b*y # Example function
set yrange [-1:1]
set xrange [-1:1]
splot for [i in a] for [j in b] f(i,j,x,y)

enter image description here

This is a gnuplot-only solution without the need for a system call:

a=""
b=""
splot "data" u (a=sprintf(" %s %f", a, $1), b=sprintf(" %s %f", b, \
$2)):(1/0):(1/0) not, for [i in a] for [j in b] f(i,j,x,y)
Miguel
  • 7,497
  • 2
  • 27
  • 46
  • A final question regarding the usage of the values in the data file: Here it is taking all combinations of a and b (why we have for 3 data rows, 9 graphs). Is there a way to use only a and b pairwise, ...so that we get one graph with a=1 and b=4, another one with a=2 and b=5 and a last one with a=3 and b=6? – Daniyal Oct 26 '15 at 08:28
  • @Daniyal Yes, if you have 3 tuples, you can do `splot ... for [i=1:3] f(word(a,i), word(b,i), x, y)`. `word(a,i)` selects the word occupying position `i` in string `a`. – Miguel Oct 26 '15 at 10:00