Search code examples
gnuplot

No variable substitution with a call command


I have a gnuplot script (template) which looks like this (a little bit shortened):

#!/bin/bash
F1=file_1
F2=file_2    
pdffile=output.pdf
#
term="terminal pdfcairo enhanced fontscale .3 lw 3" 
out="output '$pdffile'"
#
gnuplot -persist << EOF 
#
set $term
set $out
#
call "statistic-file.txt"  
#
#  ... (some formating instructions removed) 
#
plot  "$F1"  w l lt 1 lc rgb "red"   t "Graph1" ,\
      "$F2"  w l lt 1 lc rgb "green" t "Graph2"        
EOF
okular $pdffile

F1 F2 are variables for my data files. Via "call" command i try to include "statistic-file.txt" in which the variables F1 F2 are also used. This file looks like this (also shortened):

#
stats "$F1" u 2 nooutput name "Y1_"
stats "$F1" u 1 every ::Y1_index_min::Y1_index_min nooutput
X1_min = STATS_min
stats "$F1" u 1 every ::Y1_index_max::Y1_index_max nooutput
X1_max = STATS_max
#
# etc 
#
 set label  \
      front center point pt 6 lc rgb "red" ps 1.0 \
      at first X1_max,Y1_max tc rgb "red" \
      sprintf("%.0f kW / %.0f°", Y1_max, X1_max)

Executing the script, i get a error message:

"statistic-file.txt", line 2: Invalid substitution $F

Pasting the content of "statistic-file.txt" into the template file, then it works. It looks as if the variables in the second file have no relation to the variables in the template file. I prefer the 2 files solution, but how to solve it? Any help?


Solution

  • You are mixing bash variables and gnuplot variables. $F1 is a bash variable and will be substituted by bash only within your bash script. Within "statistic-file.txt", bash substitutes nothing, and gnuplot complains about the unknown $F1.

    You can try to "convert" the bash variable $F1 into a gnuplot variable datafile1 like this:

    #!/bin/bash
    
    F1="file_1"
    
    gnuplot -persist << EOF 
    
    datafile1="$F1"
    call "statistic-file.txt"
    plot datafile1 w lp
    
    EOF
    

    with the following statistic-file.txt:

    stats datafile1
    

    Then bash replaces $F1 with the respective filename and gnuplot uses its own variable datafile1, even within the subsequently called "statistic-file.txt".

    Just for completeness: The error message "Invalid substitution $F" comes from gnuplot 4, gnuplot 5 complains about "no datablock named $F1".