[Tex/LaTex] Selecting rows to be displayed with pgfplotstable

pgfplotstable

I have a table, read from a file, and I would like to display only rows which have a specific value in a given column.

Here is an ECM:

\documentclass{minimal}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread{
  num       value
  1         2
  2         5
  1         3
  3         2
  1         4
  2         1
}\data
\pgfplotstabletypeset[row predicate/.code={%
  \ifnum\pgfplotstablegetelem{#1}{num}\of{\data}=1\relax
  \else\pgfplotstableuserowfalse\fi}]{\data}
\end{document}

I would expect to seen only rows whose num value is 1. But I get an error when compiling:

ERROR: Missing number, treated as zero.

--- TeX said ---
 
                   \begingroup 
l.14 ...after\pgfplotstableuserowfalse\fi}]{\data}

Do you have any idea on what to do so that only specific rows are displayed?

Best Answer

You're almost there! However, the macro \pgfplotstablegetelem doesn't "evaluate" to the value you're after, but rather stores it in a macro called \pgfplotsretval. So your code works if you write it like

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread{
  num       value
  1         2
  2         5
  1         3
  3         2
  1         4
  2         1
}\data
\pgfplotstabletypeset[row predicate/.code={%
  \pgfplotstablegetelem{#1}{num}\of{\data}
  \ifnum\pgfplotsretval=1\relax
  \else\pgfplotstableuserowfalse\fi}]{\data}
\end{document}

Related Question