Discussion:
[Pgfplots-features] Simple calculations on columns of data
Martin Heller
2012-03-23 11:37:28 UTC
Permalink
I would like to perform some simple calculations on columns of data that
I am importing from external files, typesetting with pgfplotstable, and
plotting with pgfplots.

I can do the calculations using \pgfplotstablecreatecol but I can't
figure out how to report the result without typesetting the entire
column of intermediate calculations. In the example below I would like
to find the maximum, minimum, sum and average of the data in the Val
column and be able to use the results outside the table. In the example
the results are in the final row of the corresponding columns.

Can I typeset the results outside the table with the tools provided by
pgfplots/pgfplotstable or are there an easier/better way to perform such
calculations once the data has been read with \pgfplotstableread?

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage{filecontents}

\begin{filecontents*}{testdata.table}
Val
2
1
3
\end{filecontents*}

\pgfplotstableread{testdata.table}{\testdata}
\pgfplotstablecreatecol[%
create col/assign/.code={%
\getthisrow{Val}\entry
\ifnum\pgfplotstablerow>0
\pgfmathsetmacro{\entry}{max(\entry,\pgfmathaccuma)}%
\fi
\edef\pgfmathaccuma{\entry}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}]{Max}\testdata

\pgfplotstablecreatecol[%
create col/assign/.code={%
\getthisrow{Val}\entry
\ifnum\pgfplotstablerow>0
\pgfmathsetmacro{\entry}{min(\entry,\pgfmathaccuma)}%
\fi
\edef\pgfmathaccuma{\entry}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}]{Min}\testdata

\pgfplotstablecreatecol[%
create col/assign/.code={%
\getthisrow{Val}\entry
\ifnum\pgfplotstablerow>0
\pgfmathsetmacro{\entry}{add(\entry,\pgfmathaccuma)}%
\fi
\edef\pgfmathaccuma{\entry}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}]{Sum}\testdata

\pgfplotstablecreatecol[%
create col/assign/.code={%
\getthisrow{Sum}\entry
\pgfmathsetmacro{\entry}{divide(\entry,\pgfplotstablerows)}%
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}]{Avg}\testdata

\begin{document}

\pgfplotstabletypeset[%
every head row/.style={before row=\hline,after row=\hline},
every last row/.style={after row=\hline},
columns/Val/.style={fixed,fixed zerofill,precision=2},
columns/Min/.style={fixed,fixed zerofill,precision=2},
columns/Max/.style={fixed,fixed zerofill,precision=2},
columns/Avg/.style={fixed,fixed zerofill,precision=2},
columns/Sum/.style={fixed,fixed zerofill,precision=2},
]{\testdata}

\end{document}
Christian Feuersaenger
2012-03-31 18:50:31 UTC
Permalink
This post might be inappropriate. Click to display it.
Martin Heller
2012-04-02 07:30:05 UTC
Permalink
Christian Feuersaenger wrote, on 31-03-2012 20:50:

Thanks Christian for the answer and for the pgfplots package!
Post by Christian Feuersaenger
1. You are explicitly assigning columns anyway. You could assign your
temporary intermediate solutions to global variables and dereferences
that one. This is a dirty hack, I agree.
2. You can safely use \pgfplotstablegetelem{ row }{ col }\of table
where row is an integer, 0<=row<number rows and col a column name. Note
that row currently has to be a constant (not an expression). You can use
\pgfplotstablegetrowsof{ file name or \loadedtable }
to retrieve the total number of rows for some table programmatically.
Perfect. Both methods work as a solution to my problem at hand.

Continue reading on narkive:
Loading...