13

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

tshepang
  • 12,111
  • 21
  • 91
  • 136
whatsherface
  • 313
  • 2
  • 8
  • 21

3 Answers3

16

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.

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Thank you very much! I tried the columnheader method but as you said it does prevent the first row from plotting. I am now working on the second method you mentioned and I think I can get it to work but I think I'm confused about how to cast the string to a numeric type. I've added above what I've changed. – whatsherface Jun 26 '12 at 19:12
  • @whatsherface -- You're using a double-quote `"` where you should be using a backtic (located at the same button as ~ on an American keyboard) – mgilson Jun 26 '12 at 19:15
  • Ok, I fixed it now. Just to make sure, there should be double quotes _and_ backtics, is that correct? When I run it it still gives me the error: Non-numeric string found where a numeric expression was expected referring to line 6 (print K+0). This would indicate that it's not going to let me cast a string to a numeric type in that way? – whatsherface Jun 26 '12 at 19:49
  • @whatsherface -- It works for me (see my edit for the exact code I used). (gnuplot 4.4.2, OS X) – mgilson Jun 26 '12 at 19:54
  • @whatsherface -- In your script, you're never actually storing the result of the cast. – mgilson Jun 26 '12 at 20:01
  • I used exactly that code but I am still getting this error--> head: file: No such file or directory "testPlot.gnu", line 9: Non-numeric string found where a numeric expression was expected (line 9 reads: K=K+0 and testPlot.gnu is the name of my gnuplot script file) I will edit above to reflect this. – whatsherface Jun 26 '12 at 22:32
  • Your problem is that in `K=...` you're not passing the datafile name. You need to explicitly say: `head -1 testPlot.txt`. (the variable `file` doesn't get expanded here). – mgilson Jun 26 '12 at 23:52
  • Ok, I've got it working now, thank you very much! I do have 2 questions though. 1) Is there any way to get around having to explicitly put in the text file's name each time? 2) How should I change this such that it pulls off the 6th entry in the 2nd row rather than the first? – whatsherface Jun 27 '12 at 14:05
  • Alright I think I've got it all up and running perfectly now. Thank you so much for all your help! I've learned of ton of stuff through this. – whatsherface Jun 27 '12 at 18:31
  • @whatsherface -- I just recently learned a better way to do system call without all the backtic gymnastics. :-). I just figured I'd let you know. (See the final edit above). – mgilson Jul 24 '12 at 15:59
  • the system call looks great. I just tried it and get the gnuplot error message `')' expected` – DaveFar Aug 19 '15 at 21:08
  • how do I mix `columnheader` with variable. like say `title : columnheader(2)` how do I do that? – Eular Sep 16 '20 at 13:47
7

This is a very old question, but here's a nice way to get access to a single value anywhere in your data file and save it as a gnuplot-accessible variable:

set term unknown #This terminal will not attempt to plot anything
plot 'myfile.dat' index 0 every 1:1:0:0:0:0 u (var=$1):1

The index number allows you to address a particular dataset (separated by two carriage returns), while every allows you to specify a particular line.

The colon-separated numbers after every should be of the form 1:1:<line_number>:<block_number>:<line_number>:<block_number>, where the line number is the line with the the block (starting from 0), and the block number is the number of the block (separated by a single carriage return, again starting from 0). The first and second numbers say plot every 1 lines and every one data block, and the third and fourth say start from line <line_number> and block <block_number>. The fifth and sixth say where to stop. This allows you to select a single line anywhere in your data file.

The last part of the plot command assigns the value in a particular column (in this case, column 1) to your variable (var). There needs to be two values to a plot command, so I chose column 1 to plot against my variable assignment statement.

KDN
  • 1,349
  • 2
  • 14
  • 17
4

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
StackJack
  • 145
  • 1
  • 9