[Tex/LaTex] pgfplots input parse error with expression

pgfplots

Hi there I got the following problem at hand. I load my plot data automatically from an external .csv file and manipulate it via a simple expression (conversion from ms to seconds) before plotting it via pgfplots. My problem arises whenever I have empty values to plot, the expression returns

Package PGF Math Error: Could not parse input '' as a floating point
number, sorry. The unreadable part was near ''. (in '/1000').

For better understanding the MWE

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{testdata.csv}
a;b;c
5000;;2000
\end{filecontents}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[   
    legend entries = {
      \textbf{a},
      \textbf{b},
      \textbf{c},
    }
  ]  
  \addplot table[x expr=\coordindex,y expr=\thisrow{a}/1000, col sep=semicolon]{testdata.csv};
  \addplot table[x expr=\coordindex,y expr=\thisrow{b}/1000, col sep=semicolon]{testdata.csv};
  \addplot table[x expr=\coordindex,y expr=\thisrow{c}/1000, col sep=semicolon]{testdata.csv};

  \end{axis}
\end{tikzpicture}

\end{document}

In this example column b has the empty value which results in the error because of the expression. My question is how can I get around that error without inserting NaN in the empty space.

Best Answer

You might consider using a y filter instead of a y expr, as the latter does not seem to be able to cope with missing values. Before doing the calculation, one has to test if the current point is missing/empty or not. I illustrate this below, which at least works if you want to transform all the plots in the axis in the same way.

Note that some of Harish Kumar's suggestions of using \pgfplotsset{compat=1.7} and reading the table into a macro may be a good idea instead of reading the file three times etc. Here, I have made only minimal changes to your provided MWE.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{testdata.csv}
a;b;c
5000;;2000
\end{filecontents}

\def\myemptymacro{}
\newcommand*{\divideathousand}[1]
        {\ifx\pgfmathresult\myemptymacro\else\pgfmathdivide{#1}{1000}\fi}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[   
    legend entries = {
      \textbf{a},
      \textbf{b},
      \textbf{c},
    },
    y filter/.code={\divideathousand{#1}}
  ]
  \addplot table[x expr=\coordindex,y=a, col sep=semicolon]{testdata.csv};
  \addplot table[x expr=\coordindex,y=b, col sep=semicolon]{testdata.csv};
  \addplot table[x expr=\coordindex,y=c, col sep=semicolon]{testdata.csv};


  \end{axis}
\end{tikzpicture}

\end{document}

Result:

illustration of output

Note that the legend is incorrect though, because no points were kept for the second plot...