2

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
2

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}

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.