6

I have a large number of files containing xy-data that I would like to plot in one plot. I know I can do that manually and bit by bit which is ok for only several files, but not for larger numbers.

\documentclass[crop]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{filecontents}

\begin{document}
\begin{filecontents}{data01.dat}
0   0
1   1
2   2
\end{filecontents}
\begin{filecontents}{data02.dat}
0   1
1   2
2   0
\end{filecontents}
\begin{tikzpicture}
    \begin{axis}
    \addplot table [x index=0,y index=1] {data01.dat};
    \addplot table [x index=0,y index=1] {data02.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

Is there a way to plot a set of numbered files or even all files in a certain folder autimatically in pgfplots?

1 Answer 1

10

Use a \foreach loop (part of pgf):

\documentclass[crop]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{filecontents}

\begin{document}
\begin{filecontents}{data01.dat}
0   0
1   1
2   2
\end{filecontents}
\begin{filecontents}{data02.dat}
0   1
1   2
2   0
\end{filecontents}
\begin{tikzpicture}
    \begin{axis}
      \foreach \num in {01,02} {
        \addplot table [x index=0,y index=1] {data\num.dat};
      };
    \end{axis}
\end{tikzpicture}
\end{document}

For large numbers of files, you can define in terms of a series, like:

\foreach \num in {1,2,...,18} {
  \addplot table [x index=0,y index=1] {data\ifnum\num<10 0\fi\num.dat};
};

to plot data01.dat, data02.dat, data03.dat, ..., data18.dat. Thanks to egreg in the comments for the quick leading zero solution.

3
  • This seems to be a step in the right direction. It works nicely when deleting the leading zeros from the file names. A solution including (multiple) leading zeros or all files from one folder would still be appreciated, though.
    – Janek
    Aug 25, 2014 at 15:21
  • \makeatletter\let\twodigits\two@digits\makeatother in the preamble and data\twodigits{\num}.dat in the \foreach code should do.
    – egreg
    Aug 25, 2014 at 15:22
  • 3
    Or, directly, data\ifnum\num<10 0\fi\num.dat
    – egreg
    Aug 25, 2014 at 15:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .