# How can I filter/select data (float, text) from a table and plot it?

Note

I post this question in addition to a recent question (see below) with a more detailed description of the problem:

This was the original question: How can I filter/select data from a table and plot it?

Description

Now, this is the specific problem: I have a table with data and before anything I sort the data by one of the columns. Then I would like to do the following

• filter only the rows that contain a specific text
• filter again, but now only the rows that contain a specific value (which may also be a float number or an empty cell)
• plot the filtered data as a table
• plot one of the columns with the filtered data as x and another one as y in a diagram.

Here is an example of my table and the code to sort it:

\documentclass{minimal}
\usepackage{pgfplotstable}
\usepackage{pgfplots}

\begin{document}
%table with data, empty cells contain 'nan'
L   C   T   D   V
A   0   20  1.00346 205
A   0   23  1.00118 177
A   0   25  0.99964 155
A   0   30  0.99579 115
A   0   40  0.98807 69.5
A   9.2 20  0.98444 nan
A   9.2 30  0.97664 66
A   9.2 40  0.96862 43
A   30  20  0.96936 nan
A   30  23  0.96622 16.1
A   30  30  0.964   15.3
B   0   20  1.05104 535
B   0   23  1.04876 439
B   0   25  1.0472  373
B   0   30  1.04327 234
B   0   40  1.03555 115
B   9.6 20  1.02981 nan
B   9.6 23  1.02749 152
B   9.6 25  1.0259  134
}\data

\pgfplotstablesort[sort key={T}]{\sorted}{\data} %get the data and sort by column 'T'

\end{document}


Now I want to filter the first column L to select the desired sample. Then I want to filter again the remaining data by column C to get the desired property. In the question that I posted recently this turned out to be a problem because I simplified the problem too much as it is important if the column contains text, integer or float values.

The generated code for filtering one column with float values (here: column C) while plotting the diagram looked like that:

\begin{tikzpicture}
\begin{axis}[
xlabel=T,
ylabel=D,
x filter/.code={\pgfplotstablegetelem{\coordindex}{C}\of{\sorted}
\pgfmathtruncatemacro{\tempva}{abs(\pgfplotsretval - 9.2) == 0 ? 1: 0}
\ifnum\tempva>0%true
\else%false
\def\pgfmathresult{}
\fi
},
]
\end{axis}
\end{tikzpicture}


Note: In my original question I erased the first column L to simplify the problem as it contains text.

So now, sorting works fine but filtering seems to be my problem.

The question is: How can I set 2 filters and finally plot the remaining data as a table to have a whole overview and then plot a diagram by chosing my desired columns with the data.

-

I have used the xstring package for text comparison which can also be used to compare numbers if the number of digits are fixed.

EDIT Added the missing table typesetting part. Filtered B-9.6 entries for the table and A-9.2 entries for the plot

\documentclass{article}
\usepackage{pgfplotstable,xstring}

\begin{document}
L   C   T   D   V
A   0   20  1.00346 205
A   0   23  1.00118 177
A   0   25  0.99964 155
A   0   30  0.99579 115
A   0   40  0.98807 69.5
A   9.2 20  0.98444 nan
A   9.2 30  0.97664 66
A   9.2 40  0.96862 43
A   30  20  0.96936 nan
A   30  23  0.96622 16.1
A   30  30  0.964   15.3
B   0   20  1.05104 535
B   0   23  1.04876 439
B   0   25  1.0472  373
B   0   30  1.04327 234
B   0   40  1.03555 115
B   9.6 20  1.02981 nan
B   9.6 23  1.02749 152
B   9.6 25  1.0259  134
}\data
\pgfplotstablesort[sort key={T}]{\sorted}{\data} %get the data and sort by column 'T'

\pgfplotstabletypeset[row predicate/.code={%
\pgfplotstablegetelem{#1}{L}\of{\sorted}
\IfStrEq{\pgfplotsretval}{B}{%TRUE
\pgfplotstablegetelem{#1}{C}\of{\sorted}
\pgfmathtruncatemacro{\tempva}{abs(\pgfplotsretval - 9.6) == 0 ? 1: 0}
\ifnum\tempva>0%true
\else%false
\pgfplotstableuserowfalse
\fi}
{%FALSE
\pgfplotstableuserowfalse
}
},
columns/L/.style={string type}
] {\sorted}

%
\begin{tikzpicture}
\begin{axis}[
xlabel=D,
ylabel=V,
x filter/.code={\pgfplotstablegetelem{\coordindex}{L}\of{\sorted}
\IfStrEq{\pgfplotsretval}{A}{
\pgfplotstablegetelem{\coordindex}{C}\of{\sorted}
\pgfmathtruncatemacro{\tempva}{abs(\pgfplotsretval - 9.2) == 0 ? 1: 0}
\ifnum\tempva>0%true
\else%false
\def\pgfmathresult{}
\fi}
{
\def\pgfmathresult{}
}
},
x tick label style={/pgf/number format/precision=3}
]

Wow, really thank you. That works fine. I tried to use the same code now to plot the table, but there I got an error: Runaway argument?. I used it as following: \pgfplotstabletypeset[row predicate/.code={% \pgfplotstablegetelem{#1}{L}\of{\sorted} \IfStrEq{\pgfplotsretval}{A}{ \pgfplotstablegetelem{C}\of{\sorted} \pgfmathtruncatemacro{\tempva}{abs(\pgfplotsretval - 9.2) == 0 ? 1: 0} \ifnum\tempva>0%true \else%false \def\pgfmathresult{} \fi}] {\sorted} Isn't that correct? –  wonder Aug 12 '12 at 18:37
@wonder See the edit for the table. There are a few differences between table row predicate and x filter such as: \coordindex belongs to pgfplots but not to the pgfplotstable etc. –  percusse Aug 12 '12 at 19:14
Thank you. I see, that's why I have to use \pgfplotstableuserowfalse instead. –  wonder Aug 13 '12 at 7:37