Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 278
whatsherface Avatar asked Jun 26 '12 15:06

whatsherface


2 Answers

Here is a less 'awk'-ward solution which assigns the value from the first row and 6th column of the file 'Data.txt' to the variable x16.

set table
# Syntax: u 0:($0==RowIndex?(VariableName=$ColumnIndex):$ColumnIndex)
# RowIndex starts with 0, ColumnIndex starts with 1
# 'u' is an abbreviation for the 'using' modifier 
plot 'Data.txt' u 0:($0==0?(x16=$6):$6)
unset table

A more general example for storing several values is given below:

# Load data from file to variable
# Gnuplot can only access the data via the "plot" command
set table
# Syntax: u 0:($0==RowIndex?(VariableName=$ColumnIndex):$ColumnIndex)
# RowIndex starts with 0, ColumnIndex starts with 1
# 'u' is an abbreviation for the 'using' modifier 
# Example: Assign all values according to: xij = Data33[i,j]; i,j = 1,2,3
plot 'Data33.txt' u 0:($0==0?(x11=$1):$1),\
                '' u 0:($0==0?(x12=$2):$2),\
                '' u 0:($0==0?(x13=$3):$3),\
                '' u 0:($0==1?(x21=$1):$1),\
                '' u 0:($0==1?(x22=$2):$2),\
                '' u 0:($0==1?(x23=$3):$3),\
                '' u 0:($0==2?(x31=$1):$1),\
                '' u 0:($0==2?(x32=$2):$2),\
                '' u 0:($0==2?(x33=$3):$3)
unset table
print x11, x12, x13     # Data from first row
print x21, x22, x23     # Data from second row
print x31, x32, x33     # Data from third row
like image 106
StackJack Avatar answered Oct 06 '22 10:10

StackJack


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.

like image 27
mgilson Avatar answered Oct 06 '22 11:10

mgilson