Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

How to define and access array in GNUplot?

This is rather easy question or maybe too easy question. But i tried to find the way to done these already and could not find even in GNUplot document. Might be my mistake or misunderstood something about array concept in GNUplot. My question is How to define and access array in GNUplot?

Please just provide easy example of array declaration, assign value of array over loop. i think that's enough and i think this will be useful for other people too.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are using Gnuplot 5.1 or superior and need a 1-d array, you simply define the array with size N, remembering that the indices go from 1 to N:

gnuplot> array A[3] #Array definition
gnuplot> A[1]=2
gnuplot> A[3]=4
gnuplot> print A[1]
2
gnuplot> print A    #Print the array, with empty A[2]
[2,,4]

If you need more than one dimension or are using previous versions of Gnuplot, you can do the following:

Since there are no vector variables in previous versions of Gnuplot, two functions can be defined to get and set values to a behind the scenes variable whose name include the index. The functions are:

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

To assign and retrieve values on the array A you do

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

What these functions do is to access a variable called _A_2.

You can build similar function to work 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) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...