Martin Heller
2012-03-23 11:37:28 UTC
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}
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}