# Get the number of columns of a table

Consider the following plots

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

1 3 6 4
2 2 5 2
3 4 4 1
4 5 2 3
}\table

\begin{tikzpicture}
\begin{axis}
\foreach \i in {1,...,3}{%
}
\end{axis}
\end{tikzpicture}

\end{document}


I would like to use it as a template for input files of varying number of columns. For this I need to replace the 3 ine the foreach command by the number of columns of the loaded table. Is there something similar to \pgfplotstablecols that would be understood in this environment?

-

There is \pgfplotstablegetcolsof{<table>}, which stores the number of columns of the given table in the macro \pgfplotsretval. You'll need to subtract one from this number to get the correct index of the last column:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

1 3 6 4 2
2 2 5 2 1
3 4 4 1 5
4 5 2 3 1
}\table

\begin{tikzpicture}
\begin{axis}
\pgfplotstablegetcolsof{\table}
\pgfmathparse{\pgfplotsretval-1}
\foreach \i in {1,...,\pgfmathresult}{%