3
votes

I'm using Gnuplot with scripts and data files.

In my script there is a command;

set title "blah title here"

Is it possible to have that string taken from a data file? e.g. such that I can use a single script with many data files, because the data file will contain the title for the plot.

2
Thanks for your answers guys; I think the conclusion is you basically can't, and so I'm now taking the approach of using in-line data (e.g. data in the gnuplot script itself). - user82238
having the tool which produces the data also produce the gnuplot script to plot it is a method which I've resorted to from time to time. Good luck. - mgilson

2 Answers

5
votes

I'm not sure if this would be easy to do in pure gnuplot, but here is a solution using a wrapper bash script. You would use the script by calling plotscript.sh data.dat at the command line.

#!/bin/bash

my_title=$(head -n 1 $1 | sed 's/^# \(.*\)/\1/')

echo "set terminal postscript enhanced color
set output 'plot.eps'

set title '$my_title'
plot '$1' u 1:2" | gnuplot

To make the script usable put the code in a textfile and run chmod +x on it. If you tell me what format the title is in I can try to tailor the script to match that. This script assumes that the title is the first line of the data file in this type of format:

# mytitle

1 4
2 5
3 2
2
votes

you can use backtic substitution...e.g.

set title "`head -1 datafile.dat`"

However, that doesn't quite get what you want since the backtic substitution is done prior to string operations (You can't specify the datafile name as a string). However, Macros are expanded prior to backtic substitutions.

My test datafile looked like:

"this is the title"
10 20
20 30
30 40

And my test script looked like:

DATAFILE="datafile.dat"
set macro
TI='`head -1 '.DATAFILE.'`'  #macro: Single quotes are important here to prevent expansion of backtics.
set title @TI
plot DATAFILE u 1:2 title columnhead(1)

Note that if your title isn't enclosed in double quotes in the datafile, you'll need to add them so that the resulting set title command is valid. (You can either add them to the macro, or to the datafile)