# How to select a finite number of samples from the file when plotting using pgfplot

I have a datafile containing thousands of points of which I need to plot only the first 100 points. How to do this using pgfplots? I tried the following with no effect.

\begin{axis}[ xlabel=k, ylabel=$R_k$, xmin=1, samples y = 100]


I also tried,

\begin{axis}[ xlabel=k, ylabel=$R_k$, xmin=1 ]
\addplot table [x expr=\coordindex+1,y index=0,samples y = 100] datafile.txt};


Any suggestions?

-
what about removing the unused data points from the text files? It's probably easier. –  pluton Mar 12 '12 at 23:22
thanks. That is what I am doing now. But I want to plot a graph and and an inset graph with smaller number of data points. I want to do this for a series of examples and felt that if a pgfplots option is available (a newbie here), would prefer that instead of creating large number of files. –  suresh Mar 12 '12 at 23:32

When using addplot, there is an option to select every nth point:

\addplot[<your options here>,each nth point={100}] ...

-
Thank you for exposing me to this option. What I wanted was a method by which I can select the first 100 points only. I believe I can use skip coords between index={begin}{end} option except that I have to know the end value. BTW is there any keyword denoting the end value of index? –  suresh Mar 13 '12 at 2:18

With the indirect help of Mark S. Everitt, I figured out how to do this.

\addplot table [x expr=\coordindex+1,y index=0,skip coords between index={101}{2700}]{datafile.txt};


From the pgfplots manual, /pgfplots/skip coords between index={begin}{end}

A style which appends an x filter which discards selected coordinates. The selection is done by index where indexing starts with 0, see \coordindex. Every coordinate with index begin ≤ i < end will be skipped.

-
Ah, I see what you wanted now. Sorry, should have read the question more carefully. This can be improved however. You can automatically acquire the total number of rows in your table with \pgfplotstablegetrowsof{datafile.txt}\pgfmathsetmacro\nrows{\pgfmathresult}. Now instead of putting in the 2700 manually you can replace it with \nrows and know that it will work for any number of rows. –  qubyte Mar 13 '12 at 3:13
Wow, nice. I presume that \pgfmathresult would contain the result of pgfplotstablegetrowsof which you are assigning to the variable nrows using the pgfmathsetmacro command. What I did is, I gave a large number to the end value and TeX anyway didnt complain :) –  suresh Mar 13 '12 at 3:24
You're right. You can put this in right above the \addplot to get the desired result. The tools provided by pgfplotstable` mean that you can further automate this by only loading the datafile once. –  qubyte Mar 13 '12 at 6:15