I want to use system call(s) to grab values from first data line in file file and print into legend of plot.
The command returns a syntax error. I admit come confusion about a- the use of system calls and syntax, despite reading few advanced questions here.
This is what I have:
gnuplot> plot for [i=20:30:1] '4He-processed' index i u 8:($22>0.2&&$22<2?$9:1/0):10 w yerr t system("head -2 4He-processed | tail -1 | awk '{printf "%s %8.3f %8.3f %s" , "'", $3, $4,"'"}'")
with the response: ')' expected with the pointer at the "f" in printf.
I want to have the values in $3 and $4 written to the legend.
This alternate command
gnuplot> plot for [i=20:30:1] '4He-processed' index i u 8:($22>0.2&&$22<2?$9:1/0):10 w yerr t system("head -2 4He-processed | tail -1 ")
puts the entire first line, of each index loop, to the legend
It likely has to do with syntax?
I want the values from $3 and $4, not the column headings:
Here is the some lines (but not all the columns) from the file
nz na e0 theta nu xsect ert y fy fye
2 4 0.150 60.000 0.025 0.330E 02 0.752E 00 -0.0459 0.956E 00 0.218E-01
2 4 0.150 60.000 0.030 0.497E 02 0.784E 00 -0.0001 0.146E 01 0.230E-01
2 4 0.150 60.000 0.035 0.483E 02 0.766E 00 0.0315 0.144E 01 0.229E-01
2 4 0.150 60.000 0.040 0.408E 02 0.728E 00 0.0573 0.125E 01 0.224E-01
This continues for many blocks. Here, if I were to start my loop with the first block, the values (at $3 and $4) would be 0.150 and 60.000 which correspond to the energy and angle of the projectile and they would hopefully appear in the legend. The plotted quantities ($8,$22 and $23) not pasted here (too many columns).
CodePudding user response:
The problem is that the double-quoted string in your command starts with head"
and runs to printf"
. That is clearly not what you intended.
Also I am uncertain what you mean by values in $3 and $4
. Can you show an example of the first line (2 lines?) of your file and state, in words, what you are trying to extract from it?
Are you trying to produce a single title for the entire set of 10 plots or are you trying to generate 10 separate titles? As shown, even apart from the quoting issue, the command will generate 10 identical titles.
I am guessing that maybe you want something like:
set datafile columnheaders
plot for [i=20:30:1] '4He-processed' skip 1 index i u 8:($22>0.2&&$22<2?$9:1/0):10 w yerr \
title sprintf("something about %s and %s", columnhead(3), columnhead(4))
That assumes each data block in the file starts with a line of column headers. I put in skip 1
because your system command seemed to indicate that the first line in the file was junk, but I may have misunderstood the file layout.
$DATA << EOD
#this line is junk
A B C D E
1 1 1 1 1
2 1 1 1 1
F G H I J
1 2 2 2 2
2 2 2 2 2
K L M N O
1 3 3 3 3
2 3 3 3 3
EOD
set yrange [0:5]
set datafile columnhead
plot for [i=0:2] $DATA skip 1 index i using 1:2 with lp \
title sprintf("Set %d contains %s and %s", i, columnhead(3), columnhead(4))
CodePudding user response:
I guess you can't use columnheader()
(check help columnheader
), because by this you will sacrifice the first data line of each block and I assume you do want to keep all data.
The following should work for gnuplot>=5.4.0 and under the assumption that column 3 and 4 do not change within each block. However, this will not work for gnuplot<=5.4.0, because (as I understand) the title for the legend will be evaluated before each plotting iteration and in gnuplot>=5.4.0 after each plotting iteration. For gnuplot<5.4.0 you have to think about other workarounds.
I hope you get the idea and can adapt the script to your case, i.e. filtering, etc.
Script: (works for gnuplot>=5.4.0)
### extract title from first line without using columnheader()
reset session
$Data <<EOD
nz na e0 theta nu xsect ert y fy fye
1 11 0.150 60.000 5 6 7 8 9 10
2 12 0.150 60.000 5 6 7 8 9 10
3 13 0.150 60.000 5 6 7 8 9 10
4 14 0.150 60.000 5 6 7 8 9 10
nz na e0 theta nu xsect ert y fy fye
1 21 0.250 65.000 5 6 7 8 9 10
2 22 0.250 65.000 5 6 7 8 9 10
3 23 0.250 65.000 5 6 7 8 9 10
4 24 0.250 65.000 5 6 7 8 9 10
nz na e0 theta nu xsect ert y fy fye
1 31 0.350 70.000 5 6 7 8 9 10
2 32 0.350 70.000 5 6 7 8 9 10
3 33 0.350 70.000 5 6 7 8 9 10
4 34 0.350 70.000 5 6 7 8 9 10
EOD
set key out
plot for [i=0:2:1] $Data u 1:2:(myTitle=sprintf("%g, %g",$3,$4)) index i \
w p pt 7 ps 2 lc i title myTitle
### end of script
Result: