How do you save the output from a gnuplot command into a (string) variable? For example, I need to know the current xdata format setting in a "call"ed helper script, but I cannot recall the trick to save it. e.g.
gnuplot> show xdata
x is set to time
But how do I save or gain access to this state?
I don't see any GP_VAL
variable which makes this state available, and I am sure there are other states too, which are hard to access in a script.
Many thanks :-)
In the past, I also wished that some parameters which you can display via show ...
could be put into a variable. I'm not aware that this can be done with gnuplot easily now, but maybe in the future?
As I understand your question you want to find out within the script if xdata
is set to time
or not. If you check help xdata
the setting can either be time
or numeric
.
One possible workaround to find out:
save
(check help save
), apparently you can't save it to a datablock.xdata
is set to timedata. If it's set to time the line to find is set xdata time
and if it's set to numeric the line is set xdata
only which makes it a bit more difficult to extract (maybe there is an easier way).
The file will look like this:#
# G N U P L O T
# Version 6.0 patchlevel 0 last modified 2023-12-09
#
# Copyright (C) 1986-1993, 1998, 2004, 2007-2023
# Thomas Williams, Colin Kelley and many others
#
# gnuplot home: http://www.gnuplot.info
# faq, bugs, etc: type "help FAQ"
# immediate help: type "help" (plot window: hit 'h')
# set terminal qt 0 font "Sans,10"
# set output
unset clip points
set clip one
unset clip two
unset clip radial
set errorbars front 1.000000
set border 31 front lt black linewidth 1.000 dashtype solid
set cornerpoles
set zdata
set ydata
set xdata time
set y2data
set x2data
set boxwidth
set boxdepth 0
set style fill empty border
...
Unfortunately, gnuplot is not made for parsing, but you nevertheless can do it.
Script: (requires gnuplot>=6.0 because of function block)
### return setting for xdata
reset session
function $xdata(none) <<EOF
FILE = "Settings.txt"
save FILE
set table $Dummy
plot FILE u 0:(strcol(2) eq "xdata" ? \
XDATA=(strlen(' '.strcol(3))>3 ? "time" : "numeric") : 0) w table
unset table
return XDATA
EOF
set xdata
print $xdata(0)
set xdata time
print $xdata(0)
### end of script
Result:
numeric
time
Script: (requires gnuplot>=5.0.0 because of datablocks)
### return setting for xdata
reset session
set xdata
FILE = "SO78723633.txt"
save FILE
set table $Dummy
plot FILE u 0:(strcol(2) eq "xdata" ? \
XDATA=(strlen(' '.strcol(3))>3 ? "time" : "numeric") : 0) w table
unset table
print XDATA
set xdata time
FILE = "SO78723633.txt"
save FILE
set table $Dummy
plot FILE u 0:(strcol(2) eq "xdata" ? \
XDATA=(strlen(' '.strcol(3))>3 ? "time" : "numeric") : 0) w table
unset table
print XDATA
### end of script
Result:
numeric
time
Addition:
As mentioned in the comments, the above solutions are dependent on the setting for the datafile separator. If this has been set, e.g. to set datafile separator comma
these solutions won't work.
Hence, here is a solution which is using a system dependent call, however, which works under Windows without first having to install some utilities like grep
, gawk
, coreutils
, etc. The saved script file is read 1:1 into a datablock and then checked via strstrt()
(check help strstrt
).
Script: (requires gnuplot>=5.2.0, because of indexing datablocks)
set xdata time # e.g. set in the caller script
TEMP = "Temp.txt"
save TEMP
FileToDatablock(f,d) = GPVAL_SYSNAME[1:7] eq "Windows" ? \
sprintf('< echo %s ^<^<EOD & type "%s"',d,f) : \
sprintf('< echo "\%s <<EOD" & cat "%s"',d,f) # Linux/MacOS
load FileToDatablock(TEMP,'$Settings')
do for [i=1:|$Settings|] {
if (strstrt($Settings[i],'set xdata')==1) { XDATA = $Settings[i] }
}
print XDATA
Result:
set xdata time