1

I've got to a point where I have some data stored in a pgfplotstable macro. I want to remove a row from that macro (a line where some data is missing) and then further process the remaining data (doing some calculations) before plotting it with pgfplots. What I think would work would be if I could create a new macro containing all of the original data, omitting the row where data is missing. Is this possible? Or is there a better way? This is a simplified version of the situation I'm in. In this case I would want to remove the row 2,2,,. There may be multiple lines with missing data, and I don't necessarily know exactly which ones they are.

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotstableread[col sep=comma]{
  a,b,c,d
  1,1,1,1
  2,2,,
  3,3,3,3
}\data
\begin{document}
\pgfplotstabletypeset{\data}
\begin{tikzpicture}
  \begin{axis}
    \addplot table [x=a,y=b]{\data};
  \end{axis}
\end{tikzpicture}
\end{document}

2 Answers 2

1

The answer and code provided here seems to do what I need.

Here's a sample file:

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable,pgfplotstablefilter}
\pgfplotstableread[col sep=comma]{
  a,b,c,d
  1,1,1,1
  2,2,,
  3,3,3,3
  4,4,4,4
  5,5,,
}\data
\begin{document}
\pgfkeys{/pgfplots/table/col sep=comma}
\pgfplotstabletypeset{\data}
\pgfplotstablefilter[c unequal {}]{\data}{\newdata}
\pgfplotstabletypeset{\newdata}
\begin{tikzpicture}
  \begin{axis}
    \addplot table [x=a,y=b]{\newdata};
  \end{axis}
\end{tikzpicture}
\end{document}

And here's the output:

enter image description here

1

Alas, \pgfplotstablevertcat does not support options. BTW, this assumes you know what rows to remove.

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotstableread[col sep=comma]{
  a,b,c,d
  1,1,1,1
  2,2,,
  3,3,3,3
}\data
\begin{document}
\pgfplotstablesave[skip rows between index={1}{2}]{\data}{filename}% create new file
\pgfplotstableread{filename}\data% read new file
\pgfplotstabletypeset{\data}
\begin{tikzpicture}
  \begin{axis}
    \addplot table [x=a,y=b]{\data};
  \end{axis}
\end{tikzpicture}
\end{document}

This version searches column c for missing data.

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotstableread[col sep=comma]{
  a,b,c,d
  1,1,1,1
  2,2,,
  3,3,3,3
}\data
\begin{document}
\pgfplotstablegetrowsof\data
\count1=\pgfplotsretval\relax
\loop\ifnum\count1>0\relax
  \count2=\count1
  \advance\count1 by -1
  \pgfplotstablegetelem{\count1}{c}\of\data
  \ifx\empty\pgfplotsretval\relax
    \pgfplotstablesave[skip rows between index={\count1}{\count2}]{\data}{filename}% remove this row
    \pgfplotstableread{filename}\data% read new file
  \fi
\repeat
\pgfplotstabletypeset{\data}
\begin{tikzpicture}
  \begin{axis}
    \addplot table [x=a,y=b]{\data};
  \end{axis}
\end{tikzpicture}
\end{document}
5
  • Thanks. I'd missed \pgfplotstablesave in the manual. I wonder if there is any way to do this if I don't know where the missing data might be? (Which could conceivably be more than one line.) I worry that with this approach I might get the index wrong one way or another. Commented Oct 15, 2018 at 17:47
  • 1
    You can search using \pgfplotstablegetelem, although I'm not sure how to distinguish missing data. You can remove multiple rows using \pgfplottableset[skip rows between...] in a loop. Commented Oct 16, 2018 at 0:31
  • @JohnKormylo Could you just add this to your answer? This is a very common issue, what I can not solve right now...
    – PascalS
    Commented Apr 29 at 18:02
  • 1
    @PascalS - Done. Commented Apr 29 at 20:52
  • Thank you! But I realized that this was not the solution for my problem. Could you maybe have a look at this question? :)
    – PascalS
    Commented Apr 29 at 21:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .