# Compute average value of a column with pgfplots

Based on the famous example to create a new column containing the min/max/avg... value of one column, I was wondering how to compute the average value and then to use it as a float variable in a calculus ?

• What do you want to do with the value? The sum of the values can be done via the example given on page 51 of pgfplotstable manual. You can also divide that value by the \pgfplotstablerow + 1 – percusse Jun 25 '15 at 11:50
• Finally, I have solved part of the problem by myself... Here is the corresponding code. – Erwin Jun 26 '15 at 13:16
• Great, you can answer your own question if you wish. – percusse Jun 26 '15 at 13:19
• The aim was so to compute the maximum value of a column, and only one value, to finally use it in the text. The only problem there is the ugly "dataExternalA" used in the macro. I have not succeeded to dynamically the name of table as an argument – Erwin Jun 26 '15 at 13:26

The answer is posted by OP in the question.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\newcommand{\maxVal}[1]{
\pgfkeys{/pgf/fpu}
\pgfmathsetmacro\buffer{0.0}
\pgfplotstableforeachcolumnelement{#1}\of\dataExternalA\as\cellValue{\pgfmathsetmacro{\buffer}{max(\buffer,\cellValue)}}
}

\begin{filecontents}{dataA.dat}
1 5
2 3
-1 6
4 14
5 0
6 1
\end{filecontents}

\begin{document}

% Draw

\begin{tikzpicture}
\begin{axis}
\addplot[mark=none] table[x index = 0, y index = 1]{\tableA};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}
\addplot[mark=none] table[x = level, y = error1]{\tableB};
\end{axis}
\end{tikzpicture}

% Compute