# skipping the first line of a data file for pgfplotstable

I need to skip the first row of a csv file, which is auto-generated by some magic measurement apparatus. It contains its own filename as the first line. The second line contains the column names (no problem) and then the data follows:

C:\someFolder\measure\....\measurement.csv
"F_1k", ....
134.56, ....


I have found the method of reading the whole file line by line and then writing every line but the first one to a temporary file, but that seems brutal. TH.'s answer to File input and output can be modified to do just that.

As far as I know, pgfplots and datatool can not skip lines of a csv file.

Manually removing the first line is not an option, I have several hundreds of these beasts. I also try to avoid external programs because the resulting code will be used on different platforms.

The code below adds a key to \DTLloaddb, which I called omitlines (please propose a better name): it is an integer which tells us how many lines to omit from the beginning of the file. Removing the lines is done through a \loop...\repeat construction, before the datatool really starts parsing the file (but after that file is opened).

The filecontents package is not part of the solution, but is useful to produce external files from a TeX file (and have a self-contained MWE).

\documentclass{article}
\usepackage{datatool}

% The idea is to add a key to \DTLloaddb which gives
% the number of lines that should be skipped. Then
% use etoolbox to patch the internal command \\@dtlloaddb
% place to discard some lines.
\usepackage{etoolbox}
\makeatletter
\newcount{\dtl@omitlines}
{\DTLnewdb}
{%
\loop
\ifnum \dtl@omitlines > \z@
\repeat
\DTLnewdb
}
{\PackageInfo{datahack}{Sucess!}}
{\PackageError{datahack}{Failure!}{}}
\makeatother

% Produce the csv file.
\usepackage{filecontents}
\begin{filecontents*}{\jobname.csv}
JUNK!
More junk!
1,2,3
4,5,6
\end{filecontents*}
\begin{document}
% Display it.
\DTLdisplaydb{mydata}
\end{document}

• I really like how easy the option is to use! Thanks! – Christoph Aug 9 '11 at 15:06

According to Section 4.2.2 of the pgfplots manual you can add the »skip first« option to a plot command.

\addplot file[skip first] {datafile.dat};

• That seems simpler indeed :) – Bruno Le Floch Aug 8 '11 at 10:13
• This is only useful when the file contents are plotted directly, but not if the data is used in a different way or has to be pre-processed. – Christoph Aug 9 '11 at 15:03
• Use /pgfplots/table/skip first n={integer i} for \addplot table. Example: \addplot table[skip first n=1, ... – ComFreek Sep 4 '14 at 19:59
• This does not work using \pgfplotstableread{data.dat}{\data}; and then \addplot table[skip first n=2, x index=0, y index=1] {\data}. The skip seems to be only accepted if put at the place where the file is actually loaded. Would be nice to be able to skip for individual plot commands when using the same data file! – Christoph90 Mar 29 '16 at 10:20