Skipping empty files in Gnuplot

删除回忆录丶 提交于 2019-12-08 06:42:41

问题


I am using gnuplot 4.6. Also, I know that a similar question was asked more than a year ago here. The answer to that requires to write a small bash script. I want to know if it is possible to achieve this from within gnuplot script, especially when gnuplot-4.6 has so many cool features added. I am trying to achieve something like this :

set xrange[xL:xU]
set yrange[yL:yU]
plot "file1.dat" using 1:2 w l lt 1 lw 1 lc 3,\
"file2.dat" using 1:2 w l lt 1 lw 1 lc 3

I am repeating the above process in a loop and the xrange & yrange parameters are being updated in each iteration. Also, I am saving the output of each iteration as some image file. Now, file2.dat is guaranteed to have some points in all iterations. BUT this is NOT true for file1.dat. Hence, I want gnuplot to skip plotting the file1.dat in case it is empty. Please note, that it is PERFECTLY OK in my case if no points are plotted from file1.dat.

This can be achieved easily using an if statement, provided there is some command in gnuplot to detect if a file has no points, without trying to plot it. In that case, the above code will look something like this :

set xrange[xL:xU]
set yrange[yL:yU]
if ("file.dat" not empty){
plot "file1.dat" using 1:2 w l lt 1 lw 1 lc 3,\
    "file2.dat" using 1:2 w l lt 1 lw 1 lc 3
}else {
plot "file2.dat" using 1:2 w l lt 1 lw 1 lc 3
}

Please help me formulate the 'condition' of the aforementioned if statement.

Thanks & Cheers

Abhinav


回答1:


I could not find any hack that used only gnuplot commands. As a result, I am posting a work around which takes the help of shell to find if file1.dat has any any lines of data or not.

The condition in the gnuplot script file will look like :

if(system("awk '{x++}END{ print x}' file1.dat")>0){
    plot "file1.dat" using 1:2 w l lt 1 lw 1 lc 3,\
    "file2.dat" using 1:2 w l lt 1 lw 1 lc 3
}else{
    plot "file2.dat" using 1:2 w l lt 1 lw 1 lc 3
}

I still would be grateful if anyone can give me a gnuplot command only method.



来源:https://stackoverflow.com/questions/19461549/skipping-empty-files-in-gnuplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!