Compute average value of a column with pgfplots

1,313

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}

% Read

\pgfplotstableread{dataA.dat}\tableA
\pgfplotstableread{pgfplotstable.example1.dat}\tableB

% 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

\pgfplotstableread{dataA.dat}\dataExternalA
\maxVal{[index] 0}
The maximum for the first column of tableA is: \pgfmathprintnumber{\buffer} 

\pgfplotstableread{pgfplotstable.example1.dat}\dataExternalA
\maxVal{error1}
The maximum for the column "error1" of the 'pgfplotstable.example1.dat' file is: \pgfmathprintnumber{\buffer}


\end{document}
Share:
1,313

Related videos on Youtube

Erwin
Author by

Erwin

I am an Assistant professor, whose main activities focus on modeling and simulation in the fluid mechanics and thermodynamics field

Updated on June 27, 2020

Comments

  • Erwin
    Erwin over 3 years

    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 ?

    • percusse
      percusse over 8 years
      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
    • Erwin
      Erwin over 8 years
      Finally, I have solved part of the problem by myself... Here is the corresponding code.
    • percusse
      percusse over 8 years
      Great, you can answer your own question if you wish.
    • Erwin
      Erwin over 8 years
      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