Search code examples
gnuplot

gnuplot: how to know the last column number?


I have a problem handling data using gnuplot. My data has different column number per line. I want to plot with X-axis of the first column and Y-axis of the last. The last columns are always different every line.

For example, my data looks like that (my.dat)

1 2 
2 1 3
3 4 4
4 5
5 2 1 3 6

plot 'my.dat' us 1:(lastcolumn) w l

Before reading in gnuplot, I can pre-process of the data. But my gnuplot is windows version, I cannot use awk or any parsing program. So I hope it handles only into gnuplot. Is that possible?

Thanks


Solution

  • Yes, you can check that with gnuplot. The idea is as follows:

    You analyze your data with stats and inside the using you check recursively with valid which column is the last valid. If an invalid column is reached you return the number of the previous column otherwise the next column is checked. The last column is then contained in the variable STATS_max

    check_valid_column(c) = valid(c) ? check_valid_column(c + 1) : c - 1
    stats 'my.dat' using (check_valid_column(1)) nooutput
    
    last_column = int(STATS_max)
    plot 'my.dat' using 1:last_column