Martin Heller | 23 Mar 2012 12:37
Picon
Favicon

Simple calculations on columns of data

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}
(Continue reading)

Christian Feuersaenger | 31 Mar 2012 20:50

Re: Simple calculations on columns of data

Hello Martin,

I believe there are two solutions:

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.

Some side-note: make sure that you terminate your code lines by '%' -- 
otherwise, TeX considers the newline character to be a whitespace and 
produces spurious white space in your table (or shifts the table).

Here would be your code:

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

\begin{filecontents*}{testdata.table}
Val
2
(Continue reading)

Martin Heller | 2 Apr 2012 09:30
Picon
Favicon

Re: Simple calculations on columns of data

Christian Feuersaenger wrote, on 31-03-2012 20:50:

Thanks Christian for the answer and for the pgfplots package!

>
> I believe there are two solutions:
>
> 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.

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here 
http://p.sf.net/sfu/sfd2d-msazure

Gmane