nerdizzle
nerdizzle

Reputation: 456

store commented value from data file in gnuplot

I have multiple data files output_k, where k is a number. The files look like

    #a=1.00 b = 0.01
    # mass  mean    std  
    0.2 0.0163  0.0000125
    0.4 0.0275  0.0001256

Now I need to retrieve the values of a and b and to store them in a variable, so I can use them for the title or function input etc. The looping over the files in the folder works. But I need some help with reading out the the parameters a and b. This is what i have so far.

    # specify the number of plots
    plot_number = 100
    # loop over all data files
    do for [i=0:plot_number] {
    a = TODO
    b = TODO
    #set terminal
    set terminal postscript eps size 6.4,4.8 enhanced color font 'Helvetica,20' linewidth 2
    set title "Measurement \n{/*0.8 A = a, B = b}"

    outFile=sprintf("plot_%d.eps", i)
    dataFile=sprintf("output_%d.data", i)

    set output outFile
    plot dataFile using 1:2:3 with errorbars lt 1 linecolor "red", f(a,b)

    unset output
    }

EDIT:

I am working with gnuplot for windows.

Upvotes: 0

Views: 223

Answers (2)

theozh
theozh

Reputation: 26123

A late answer, but since you are working under Windows you either install the comparable utilities or you might be interested in a gnuplot-only solution (hence platform-independent).

  • you can use stats to extract information from the datablock (or file) to variables. Check help stats.
  • the extraction of your a and b depends on the exact structure of that line. You can split a line at spaces via word(), check help word and get substrings via substr() or indexing, check help substr.

Script: (works with gnuplot>=5.0.0)

### extract information from commented header without external tools
reset session

$Data <<EOD
#a=1.00 b = 0.01
# mass  mean    std  
0.2 0.0163  0.0000125
0.4 0.0275  0.0001256
EOD

set datafile commentschar ''
set datafile separator "\t"
stats $Data u (myHeader=strcol(1)[2:]) every ::0::0 nooutput
set datafile commentschar    # reset to default
set datafile separator       # reset to default

a = real(word(myHeader,1)[3:])
b = real(word(myHeader,4))
set label 1 at graph 0.1,0.9 sprintf("a=%g\nb=%g",a,b)

plot $Data u 1:2 w lp pt 7 lc "red"
### end of script

Result:

enter image description here

Upvotes: 0

Wrzlprmft
Wrzlprmft

Reputation: 4434

If you are on a Unixoid system, you can use system to get the output of standard command line tools, namely head and sed, which again allow to extract said values form the files:

a = system(sprintf("head -n 1 output_%i.data | sed \"s/#a=//;s/ b .*//\"", i))
b = system(sprintf("head -n 1 output_%i.data | sed \"s/.*b = //\"", i))

This assumes that the leading spaces to all lines in your question are actually a formatting mistake.

Upvotes: 1

Related Questions