Search code examples
gnuplot

How can I iterate through two file lists at the same time


I am trying to generate a bunch of (multi)plots from data in two different directories, call them dirA, and dirB. I'm using multiplot, and I'd like each one to look something like this (forgive my artistry)...

enter image description here

where plot A is generated from a data file in dirA and plot B is from dirB.

I've tried this (simplified a bit)...

filesA = system("ls dirA/*.dat")
filesB = system("ls dirB/*.dat")

i=0
do for [fn in filesA]{
    set output 'anappropriatefilename.png'

    set multiplot layout 1,2 rowsfirst

    set size 0.25,1.0
    plot fn using 1:2 with lines

    set multiplot layout 1,2 rowsfirst
    set size 0.75,1.0
    plot filesB[i] 1:2 with lines

    i=i+1

    unset multiplot
} 

but this gives me a

':' expected

error on the line

plot filesB[i] 1:2 with lines

So maybe I just don't know how to correctly reference the filesB array with an index?

Or maybe there's a better way to do this?

I hope I explained my problem well, any suggestions are welcome

Thanks


Solution

  • You have files A1,A2,...An and B1,B2,...Bm. Is m always equal to n? What should the output file(s) be named C1,C2,...Cn?

    The following example works under windows. Hopefully, you can adapt it for Linux.

    Code:

    ### create multiplots from different filelists (Windows)
    reset session
    unset multiplot
    
    myDirA = 'dirA\'
    myDirB = 'dirB\'
    myType = '*.dat'
    filesA = system('dir /b '.myDirA.myType)  # Windows
    filesB = system('dir /b '.myDirB.myType)  # Windows
    # spaces in path or filenames probably will create problems and would require a workaround
    
    set terminal pngcairo size 800,200 font ",8"
    # Assumption: number of filesA and number of filesB are identical
    #             and no spaces in path or filename
    do for [i=1:words(filesA)] {
        set output sprintf("myPlot%03d.png",i)
    
        set multiplot layout 1,2
        set origin 0,0
        set size 0.25, 1.0
        plot myDirA.word(filesA,i) u 1:2 w lines
    
        set origin 0.25, 0
        set size 0.75, 1.0
        plot myDirB.word(filesB,i) u 1:2 w lines
    
        unset multiplot
        set output
    }
    ### end of code