Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to change gnuplot variable from windows command prompt/batch file

931 views
Skip to first unread message

ChrisL

unread,
Oct 21, 2009, 11:35:13 AM10/21/09
to
I am trying to write a windows batch file to loop through all files in
a particular folder with *.dat extension. With each file I want to
run a gnuplot script to generate a series of plots. The names of the
files change so it is not possible for me to hard code file names in
gnuplot script. The number of files changes also.

I have tried to do something like this from my bat file:

for %%x in (dir /b "*.dat") do gnuplot "createplots.gp" filename="%%x"
-persist

But i cant get gnuplot to recognize filename, and change the variable
in gnuplot environment.

Is it even possible to change a variable in gnuplot from windows batch
file? I haven't been able to find any information as to whether it
can or cannot do this.

My other alternative would be to create a list of the *.dat files in a
text file using bat file. And then from gnuplot script, read in the
filename one at a time, and execute the commands with each filename.
But it I have been through gnuplot manual, web searches, etc, and I
have not been able to figure out how to solve my problem by either
method (or if its impossible).

Any help would be greatly appreciated. Let me know if I haven't
explained my problem sufficiently.

Zoltan

unread,
Oct 21, 2009, 2:45:21 PM10/21/09
to
run a gnuplot script to generate a series of plots.  The names of the
> files change so it is not possible for me to hard code file names in
> gnuplot script.  The number of files changes also.
>
> I have tried to do something like this from my bat file:
>
> for %%x in (dir /b "*.dat") do gnuplot "createplots.gp" filename="%%x"
> -persist

It is not clear what you mean by "generating" plots: do you just want
to see them on the screen? Your -persist options seems to indicate
that. If that is the case, you could simply re-name your files to
'dat.dat' before plotting, and hard-wire dat.dat in your gnuplot
script.
If that is not the case, could you also post your createplots.gp
script, or at least, the relevant parts?
Zoltán

ChrisL

unread,
Oct 21, 2009, 3:19:24 PM10/21/09
to


Thanks for your reply. I am creating the plots as PNG, the -persist
is not necessary. Here is createplots.gp:

# first set filename = '.....' in gnuplot command...then run batch
file
cd 'C:\_001_WORK\_001_Clients\Data\Parametric_Simulation_Results'


#Global Settings
###########################################################################################
set terminal png
set nokey
###############################################################################################################


# Defect Size
###############################################################################################
f1(x) = a*x+b
fit f1(x) filename using 12:26 via a,b
set title filename
set output filename."_DefectSize".".png"
set xlabel "Defect Size (%HA)"
set ylabel "Allowable Impact (kips)"
set label 1 "f=ax+b" at 51, 430 textcolor rgb "blue"
set label 2 "a=%g", a at 53,410 textcolor rgb "blue"
set label 3 "b=%g", b at 53, 390 textcolor rgb "blue"
plot filename using 12:26 with points pointtype 5 pointsize 0.15,f1(x)
with lines lt 1 lw 3 lc rgb "blue"
###############################################################################################################

# Headloss
##################################################################################################
f2(x) = a*x+b
fit f2(x) filename using 13:26 via a,b
set title filename
set output filename."_HeadLoss".".png"
set xlabel "HeadLoss (%HA)"
set ylabel "Allowable Impact (kips)"
set label 1 "f=ax+b" at 21, 430 textcolor rgb "blue"
set label 2 "a=%g", a at 23,410 textcolor rgb "blue"
set label 3 "b=%g", b at 23, 390 textcolor rgb "blue"
plot filename using 13:26 with points pointtype 5 pointsize 0.15,f2(x)
with lines lt 1 lw 3 lc rgb "blue"
###############################################################################################################

# Foundation Stiffness
#######################################################################################
f3(x) = a*x+b
fit f3(x) filename using 17:26 via a,b
set title filename
set output filename."_FoundationStiffness".".png"
set xlabel "Foundation Stiffness (kip/in/in)"
set ylabel "Allowable Impact (kips)"
set label 1 "f=ax+b" at 5100, 430 textcolor rgb "blue"
set label 2 "a=%g", a at 5200,410 textcolor rgb "blue"
set label 3 "b=%g", b at 5200, 390 textcolor rgb "blue"
plot filename using 17:26 with points pointtype 5 pointsize 0.15,f3(x)
with lines lt 1 lw 3 lc rgb "blue"
###############################################################################################################

# RNT
#########################################################################################################
f4(x) = a*x+b
fit f4(x) filename using 19:26 via a,b
set title filename
set output filename."_RNT".".png"
set xlabel "Rail Neutral Temperature (ºC)"
set ylabel "Allowable Impact (kips)"
set label 1 "f=ax+b" at 21, 430 textcolor rgb "blue"
set label 2 "a=%g", a at 23,410 textcolor rgb "blue"
set label 3 "b=%g", b at 23, 390 textcolor rgb "blue"
plot filename using 19:26 with points pointtype 5 pointsize 0.15,f4(x)
with lines lt 1 lw 3 lc rgb "blue"
###############################################################################################################

# Service Temp
################################################################################################
f5(x) = a*x+b
fit f5(x) filename using 20:26 via a,b
set title filename
set output filename."_ServiceTemp".".png"
set xlabel "Service Temperature (ºC)"
set ylabel "Allowable Impact (kips)"
set label 1 "f=ax+b" at -5, 430 textcolor rgb "blue"
set label 2 "a=%g", a at -4,410 textcolor rgb "blue"
set label 3 "b=%g", b at -4, 390 textcolor rgb "blue"
plot filename using 20:26 with points pointtype 5 pointsize 0.15,f5(x)
with lines lt 1 lw 3 lc rgb "blue"
###############################################################################################################

Zoltan

unread,
Oct 22, 2009, 2:10:28 PM10/22/09
to

> I am creating the plots as PNG, the -persist
> is not necessary.  Here is createplots.gp...

I will look into this soon, but till then, here is an easy workaround:
hard-wire some file name, dat.dat, e.g., in your gnuplot, and let the
output be dat.png. Then all you have to do is to move your data file
to dat.dat, and then move dat.png to whatever.png, where you create
'whatever' in the batch file, not in gnuplot.
Cheers,
Zoltán

ChrisL

unread,
Oct 22, 2009, 5:23:04 PM10/22/09
to

Thanks. Thats actually a pretty clever idea. The only problem is for
my case, because of the loop, dat.dat and dat.png will keep getting
overwritten by the next loop. A workaround that i have been thinking
of is similar to your idea:

1. Hard code filename=whatever in gnuplot script
2. for each loop in windows batch file create a copy of original
gnuplot script and search and replace "filename=whatever" in gnuplot
script to current filename

for example my windows batch file looks something like (its
incomplete):

for /f %%x in ('dir /b "*.dat"') do (
copy createplots.gp createplots""%%x"".gp
:search and replace "filename=whatever" in createplots""%
%x"".gp to filename=%%x
pgnuplot load 'createplots"."%%x".".gp')

I still need to figure out how to search and replace the string
"filename=whatever" in gnuplot script using windows batch. I am
working on this. I will let you know if i make this workaround work.

Thanks very much for your help/suggestions.

Chris

ChrisL

unread,
Oct 23, 2009, 8:55:10 AM10/23/09
to

I managed to make it work...so what i have done is to put a line in
original createplots.gp that contains #INSERT_FILENAME#. I create a
new .gp file for each dat file and find #INSERT_FILENAME# and insert
filname="..." on the next line. Then i send all the newly created .gp
files to pgnuplot.

here is my batch file

for /f %%x in ('dir /b "*.dat"') do (

For /F "tokens=* delims=" %%A in (createplots.gp) Do (
Echo %%A>> createplots""%%x"".gp
If "%%A"=="#INSERT_FILENAME#" (
Echo filename="%%x">> createplots""%%x"".gp)))
:delay
ping 0.1.2.3


for /f %%y in ('dir /b "*.dat"') do pgnuplot load "createplots%%y.gp" -
persist
pause

Thanks
Chris

0 new messages