如何同时遍历两个文件列表

时间:2020-01-23 01:02:37

标签: gnuplot

我正在尝试从两个不同目录中的数据生成一堆(多个)图,它们分别称为dirA和dirB。我正在使用多重绘图,我希望每个人都看起来像这样(原谅我的艺术性)...

enter image description here

其中,图A是从dirA中的数据文件生成的,而图B是从dirB中生成的。

我已经尝试过了(简化了一点)...

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
} 

但这给了我

':'预期

在线错误

plot filesB[i] 1:2 with lines

所以也许我只是不知道如何正确地使用索引引用filesB数组?

或者也许有更好的方法可以做到这一点?

希望我能很好地解释我的问题,欢迎提出任何建议

谢谢

1 个答案:

答案 0 :(得分:2)

您有文件A1,A2,...AnB1,B2,...Bmm始终等于n吗?输出文件应命名为C1,C2,...Cn

以下示例在Windows下工作。希望您可以使其适应Linux。

代码:

### 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