Script Gnuplot on windows OS

2019-06-02 19:33发布

I would like to automate the generation of graphs from a list of data files.

More details : I have a directory that contain all my data files. I need to generate a graph for each of these files (hundreds). I have a gnuplot file to generate one graph but the name of the data file is specified into the gnuplot script.

For example :

plot 'myfile' index 1  using 1:2 with linespoints pointtype 2 linecolor rgb "green"  title "leg"

I need to be able to replace "myfile" by a variable name that can be iterated on all the files of my directory.

2条回答
Evening l夕情丶
2楼-- · 2019-06-02 19:48

You could have a (Windows) batch file with the following command:

for %%G in (*.dat) do gnuplot.exe -e "fn='%%G'" script

This will run gnuplot (and script) for every .dat file in the current directory.

The -e switch is used to input variables via command line (here the filename, surrounded by quotes in case it contains spaces).

If you want to run this command directly from the shell (as opposed to within a batch file), remove one of the % symbols from each of the two pairs.

Within the script file:

plot fn index 1  using 1:2 with linespoints pointtype 2 linecolor rgb "green"  title "leg"

If the data files have a "normalized" name (e.g. data1.dat, data2.dat...) take a look at https://stackoverflow.com/a/14947085/3235496

查看更多
贪生不怕死
3楼-- · 2019-06-02 19:55

In general you can iterate over many files inside gnuplot as follows:

set terminal pdfcairo
list = "fileA fileB fileC"

do for [file in list] {
    set output file . '.pdf'
    plot file
}

That kind of iterations requires at least version 4.6.0. In this example you have a fixed list of file names (these are not allowed to contain white spaces).

On Windows you could use

list = system('dir /b *.txt')

to get such a list containing all .txt files in the current directory. Note, that you must use wgnuplot_pipes.exe for this to work.

On a Unix system you could use

list = system('ls *.txt')
查看更多
登录 后发表回答