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
553 views
in Technique[技术] by (71.8m points)

gnuplot store one number from data file into variable

OSX v10.6.8 and Gnuplot v4.4

I have a data file with 8 columns. I would like to take the first value from the 6th column and make it the title. Here's what I have so far:

#m1 m2 q taua taue K avgPeriodRatio time
#1  2  3   4   5   6        7        8

K = #read in data here
graph(n) = sprintf("K=%.2e",n) 
set term aqua enhanced font "Times-Roman,18"

plot file using 1:3 title graph(K)

And here is what the first few rows of my data file looks like:

1.00e-07 1.00e-07 1.00e+00 1.00e+05 1.00e+04 1.00e+01 1.310 12070.00
1.11e-06 1.00e-07 9.02e-02 1.00e+05 1.00e+04 1.00e+01 1.310 12070.00
2.12e-06 1.00e-07 4.72e-02 1.00e+05 1.00e+04 1.00e+01 1.310 12070.00
3.13e-06 1.00e-07 3.20e-02 1.00e+05 1.00e+04 1.00e+01 1.310 12090.00

I don't know how to correctly read in the data or if this is even the right way to go about this.

EDIT #1

Ok, thanks to mgilson I now have

#m1 m2 q taua taue K avgPeriodRatio time
#1  2  3   4   5   6        7        8

set term aqua enhanced font "Times-Roman,18"
K = "`head -1 datafile | awk '{print $6}'`"
print K+0
graph(n) = sprintf("K=%.2e",n) 

plot file using 1:3 title graph(K)

but I get the error: Non-numeric string found where a numeric expression was expected

EDIT #2

file = "testPlot.txt"
K = "`head -1 file | awk '{print $6}'`"
K=K+0  #Cast K to a floating point number  #this is line 9
graph(n) = sprintf("K=%.2e",n)
plot file using 1:3 title graph(K)

This gives the error--> head: file: No such file or directory "testPlot.gnu", line 9: Non-numeric string found where a numeric expression was expected

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You have a few options...

FIRST OPTION:

use columnheader

plot file using 1:3 title columnheader(6)

I haven't tested it, but this may prevent the first row from actually being plotted.

SECOND OPTION:

use an external utility to get the title:

TITLE="`head -1 datafile | awk '{print $6}'`"
plot 'datafile' using 1:3 title TITLE

If the variable is numeric, and you want to reformat it, in gnuplot, you can cast strings to a numeric type (integer/float) by adding 0 to them (e.g).

print "36.5"+0

Then you can format it with sprintf or gprintf as you're already doing.

It's weird that there is no float function. (int will work if you want to cast to an integer).

EDIT

The script below worked for me (when I pasted your example data into a file called "datafile"):

K = "`head -1 datafile | awk '{print $6}'`"
K=K+0  #Cast K to a floating point number
graph(n) = sprintf("K=%.2e",n)
plot "datafile" using 1:3 title graph(K)

EDIT 2 (addresses comments below)

To expand a variable in backtics, you'll need macros:

set macro
file="mydatafile.txt"
#THE ORDER OF QUOTES (' and ") IS CRUCIAL HERE.
cmd='"`head -1 ' . file . ' | awk ''{print $6}''`"'
# . is string concatenation.  (this string has 3 pieces)
# to get a single quote inside a single quoted string
#   you need to double.  e.g. 'a''b' yields the string a'b 
data=@cmd

To address your question 2, it is a good idea to familiarize yourself with shell utilities -- sed and awk can both do it. I'll show a combination of head/tail:

cmd='"`head -2 ' . file . ' | tail -1 | awk ''{print $6}''`"'

should work.

EDIT 3

I recently learned that in gnuplot, system is a function as well as a command. To do the above without all the backtic gymnastics,

data=system("head -1 " . file . " | awk '{print $6}'")

Wow, much better.


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

...