12

I need to plot data files which contain a comment in the last line, like this:

0  2  4
1  5  7
2  9  5  
3 11  15
4 10  15
THIS WAS: TOTAL

Now, pgfplots seems to have a problem with that giving me an error message complaining about an unbalanced number of columns.

My question is: How do I tell pgfplots to ignore the last row of the data file? (I know about the skip first option of pgfplotstable. Maybe there is an equivalent for the last row that I just couldn't find in the manual.)

MWE:

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable} % For \pgfplotstableread

\usepackage{filecontents}

\begin{filecontents}{total.dos}
0  2  4
1  5  7
2  9  5  
3 11  15
4 10  15
THIS WAS: TOTAL
\end{filecontents}

\pgfplotstableread{total.dos}\total

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [no markers, red] table [x=0, y=1] {\total}; \addlegendentry{$1$}
\addplot [no markers, blue] table [x=0, y=2] {\total}; \addlegendentry{$2$}

\end{axis}
\end{tikzpicture}
\end{document}
14

The simplest way of doing this is by adding additional line comment characters. For example:

\pgfplotstableread[comment chars={T}]{total.dos}\total

Will compile properly, i.e. "T" is now considered to start a comment line.

  • Nice. Is there also a solution for disallowed words, or a solution that allows you to exclude certain line numbers? – user10274 Jan 22 '12 at 18:21
  • Particular line numbers are not so simple (as far as I know), although there are other questions knocking around on TeX.sx to do with reading particular lines. The comment chars is supposed to be a comma separated list, so I tried comment chars={THIS} but LaTeX didn't like it. May be limited to single characters. – qubyte Jan 22 '12 at 18:27
  • Thank you. I didn't know about comment chars. – Philipp Jan 22 '12 at 18:51
  • You could also prefix the lines you want ignored with a common comment character, such as #. This is pretty easy to do in an editor and leaves letters, such as T, free to show up in the data. – Sharpie Jan 22 '12 at 20:12
  • @Sharpie: If this was an option, then Philipp could simply delete those lines. I'm assuming that Philipp needs a semi-automatic way to handle these files, which can't be altered directly. – qubyte Jan 23 '12 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.