Search code examples
gnuplotmultiple-columns

Is there any reserved variable name for the last column in a data file for GNUPLOT?


I have a data file where every row has a different number of columns. I wish to plot the 1st column VS the 3rd-last column. How do I do it?


Solution

  • A late answer, but a working gnuplot-only answer. You can do it without external tools to ensure platform-independence. Since you don't give any details about your data, I assumed that there are rows with variable number of columns and the latter are separated by space.

    The "trick" is to set the datafile separator to "\n" which will read a line as a whole. If the data separator is space, you can split your line with word (check help word). For each line you can determine the number of words (or "columns") via words. Check the following script and result which will plot the first column versus the 3rd last column.

    Script:

    ### plot first column vs. the 3rd last column (with variable number of columns)
    reset session
    
    $Data <<EOD
    1  2   3   4   5
    2  2   3   4   5   6   7
    3  2   3   4   5   6   7   8   9
    4  2   3   4   5   6
    5  2   3   4   5   6   7   8
    6  2   3   4
    7  2   3   4   5   6   7
    8  2   3   4   5
    EOD
    
    set datafile separator "\n"
    ColFromLast(n)  = real(word(strcol(1),words(strcol(1))-n+1))
    ColFromFirst(n) = real(word(strcol(1),n))
    
    plot $Data u (ColFromFirst(1)):(ColFromLast(3)) w lp pt 7 lc "red" \
             title "Column 1 vs. 3rd from last"
    ### end of script
    

    Result:

    enter image description here