0
votes

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?

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
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

1 Answers

1
votes

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)