gnuplot - How can I count the number of entries in a csv file column?

5k views Asked by At

I have a gnuplot code which includes a function fitting routine.

This routine is slow when a large number of data points must be fitted. The number of data points in my input file is variable depending on the parameters of some simulations I am running.

I wish to fit only 100 points. Up until now I have been doing this by manually computing the number of entries in my input file and dividing by 100 and using the resulting number as the "every N" command to the fitting command.

More detail:

The fitting command I am using is:

fit f(x) "output.csv" every N using 1:4:9 via a,b

Where N = integer_round_down(output_file_length / 100.0) - I calculate this manually on a calculator before replacing the value of N manually in my gnuplot script. (Okay, so dividing by 100, I do that in my head, not on a calculator.)

Is there any way that I can get the number of entries in any of the columns, eg col 1, col 4 or col 9... Then use variables in my script to calculate N without having to edit my script everytime I change my simulation parameters?

1

There are 1 answers

2
Miguel On BEST ANSWER

You can use the solution offered in the comments with a system() call, or use stats:

Option 1:

N = floor(system("wc -l output.csv")/100.)

Option 2:

stats output.csv
N = floor(STATS_records/100.)