0

I am having series of files (in same directory) with simple 2 column data, all named file-%d where %d is current date (so file-20200721, file-20200720 etc) and I would like to plot all of them via one script and get png outputs with same names as input files (file-20200721.png).

So far I have been trying this:

set term pngcairo enhanced size 1024, 768
set xdata time
set timefmt "%H:%M:%S"
set xrange ["00:00:00":"23:59:59"]
set format x "%H:%M:%S"
set grid
do for [i=20200101:21000101] {
    fname_in = sprintf("file-%d.log",i)
    fname_out = sprintf("file-%d.png",i)
    set output fname_out
    splot fname_in u 1:2
}
set output

but it doesn't set correct output at all (only one output, not correct file name and not real png).

Any help is welcome.

1
  • ah, so the splot was mistake (plot would create proper graph) but gnuplot just stops when it finds first file that doesn't exist. How do I skip files that don't exist? Commented Jul 21, 2020 at 14:42

2 Answers 2

1

Linux version of the loop that tests for existence of the file before trying to plot it.

do for [i=20200101:20200111] {
    fname_in = sprintf("file-%d.log",i)
    fname_out = sprintf("file-%d.png",i)
    test_command = "if [ -e " . fname_in . " ]; then echo 'exists'; fi"
    if (system(test_command) ne "exists") {
       print "Missing file: ", fname_in
       continue
    }
    set output fname_out
    splot fname_in u 1:2
}
set output
Sign up to request clarification or add additional context in comments.

1 Comment

Uh, this is the one I need. Thanks! While you at it, is there a way to skip already plotted ones?
1

Are you sure you want to loop from 20200101 to 21000101 and check 800'000 potential filenames? Maybe you mean 20210101? Anyway, I'm not aware that gnuplot has a file_exist() function. For this, I guess you have to go to operating system level.

You can do a system call (check help system) and get a list of files which obbey your naming convention. I guess path and files should not contain empty spaces (otherwise you would have to put the list items into quotes).

Code:

### loop files in a directory and plot to PNG
reset session
set term pngcairo

myPathWindows = 'C:\user\data\file-*.log'      # for Windows
LIST = system('dir /b "'.myPathWindows.'"')

# myPathLinux = 'C:/user/data/file-*.log'      # for Linux
# LIST = system('ls "'.myPathLinux.'"')

do for [FILE in LIST] {
    set output FILE[1:strlen(FILE)-4].".png"    # take input filename and change extension to ".png"
    plot FILE u 1:2 
}
set output
### end of code

3 Comments

theozh: The "set output" in the next iteration will close the previous file automatically. It is only the very last one that needs to be closed separately. So the original code as shown is correct.
@Ethan, thank you for mentioning this. I will correct it in my code. Could you please confirm whether the Linux directory listing is correct?
theozh: The slashes in the path go the wrong way. But see separate answer for a way to test for the existence of the file so that you can skip the missing ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.