3

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?

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • Not sure how you can have a different number of entries in column 1 from column 4 in a csv file? Can you use `system("wc -l < output.csv")` to get the number of lines in your csv? – Mark Setchell Jun 12 '15 at 12:48
  • @MarkSetchell Sorry if that was ambiguous - they all have the same number of entries – FreelanceConsultant Jun 12 '15 at 12:50

1 Answers1

5

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.)
Miguel
  • 7,497
  • 2
  • 27
  • 46
  • If my `output.csv` has a header line, must I also subtract 1 from `STATS_records`? – FreelanceConsultant Jun 16 '15 at 13:31
  • 1
    If gnuplot interprets your header as non data (e.g., it has a comment sign `#`) then you don't need to subtract. Otherwise you need to check: try with a small data file with the same header as your `output.csv`, run `stats` on it and then `print STATS_records` and see what you get. – Miguel Jun 16 '15 at 13:41