Search code examples
gnuplot

Output figures to specific folder in Gnuplot


I use gnuplot in Linux to plot residuals. I have all the input files named as (in1, in2, ...) in the Folder1, and I want to save all the output figures named as (in1.png, in2.png, ...) to Folder2.

This is my code:

# reaed inputs
FILES = system("ls ./Folder1/in*")

# create output folder
system "mkdir Folder2"
myOutput(s) = sprintf("%s.png",s)

# loop
do for [FILE in FILES] {
    set output myOutput(FILE) 
    plot "< awk '/for p/ {print $8}' ".FILE with lines
}

I try to modify the command set output myOutput(FILE), like set output './Folder2/myOutput(FILE)', but then the output files would named as myOutput(FILE). I also try './Folder2'.myOutput(FILE) but it generate the error cannot open file; output not changed.

Could anyone help me to solve this problem? Thanks!


Solution

  • Under the assumption that you are using Linux, you can use the command basename to extract only the last “in1" from the pathname ". /Folder1/in1".

    % basename ./Folder1/in1
    in1
    

    If you permit to use this command, your code can be modified as follows.

    # reaed inputs
    FILES = system("ls ./Folder1/in*")
    
    # create output folder
    system "mkdir -p Folder2"
    myOutput(s) = sprintf("./Folder2/%s.png",s)
    
    # loop
    do for [FILE in FILES] {
        BASENAME = system("basename ".FILE)
        set output myOutput(BASENAME) 
        plot "< awk '/for p/ {print $8}' ".FILE with lines
    }