How to detect and access an array in GNUplot?

This is a pretty simple question, or maybe a too simple question. But I tried to find a way to do this already and could not even find it in the GNUplot document. Maybe my mistake or misunderstanding of something about the concept of array in GNUplot. My question is: How to detect and access an array in GNUplot?

Please just provide a simple example of declaring an array, assign the value of the array over the loop. I think that is enough, and I think that it will be useful for other people as well.

+7
arrays gnuplot
source share
3 answers

(This answer will be deprecated with the next stable version of gnuplot, since the 5.1 development tree now has built-in support for array variables.)

The gnuplot splot command uses the array keyword to determine the size of the NxM matrix, which contains the function values ​​for the 3D plot.

Arrays like what a programmer knows from C, Pascal, Python, etc., today do not exist in gnuplot (gp5.0). They can be implemented one day, because they will be very useful for building a family of curves with arbitrary (for example, set) parameters.

If you desperately need arrays in gnuplot, you can (ab) use the word() function (and other string functions) to achieve some limited replacement. This is also a bit cumbersome:

 array = "" f(a,x) = a*x do for [i=1:5] {array = array.sprintf(" %.3f",i+rand(0)) } print "array = ".array set xr [0:]; set yr [0:30] plot for [i=1:5] f(word(array,i),x) title word(array,i)." x" 

This example writes a set of random numbers to a string variable named "array", and then uses it to build five linear functions that use the numbers in the "array" to tilt it.

+8
source share

As Carl said, there are no vector variables in Gnuplot. I use a different approach to get around this limitation. I define 2 functions that get and set values ​​for a variable backstage whose name includes an index. Functions:

 aGet(name, i) = value(sprintf("_%s_%i", name, i)) aSet(name, i, value) = sprintf("_%s_%i = %.16e", name, i, value) 

To assign and receive values ​​in array A, you do

 eval aSet("A",2,3) print aGet("A",2) 

What these functions do is access the variable named _A_2.

You can build a similar function for working with matrices:

 mGet(name, i, j) = value(sprintf("_%s_%i_%i", name, i, j)) mSet(name, i, j, value) = sprintf("_%s_%i_%i = %.16e", name, i, j, value) 
+7
source share

Inspired by @Karl's answer, it looks even more like an array when the word function is added to another function:

 array(n) = word("1 2 3 4 5 6 7 8 9", n) print array(3) 

Prints 3 . Thus, indexing is based on one thing.

Multiply the array by 2:

 print (b="", sum[i=1:9](b=b.(array(i)*2)." ", 0), b) 

Prints 2 4 6 8 10 12 14 16 18 . Here, the sum function is (ab) used to loop over the array, and its result is ignored.

And here is shorter, through a more general @bmello answer:

 A_1=1.1; A_2=2.2; A_3=3.3 A(i) = value("A_".i) print A(3) 

It seems more intuitive to me. The underscore _ can be seen simply as a function of typing. Also, this is not limited to whole indices. Lines are also possible that give some behavior similar to a dictionary.

0
source share

All Articles