# pgfplots input parse error with expression

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.

-

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}}
]

\end{axis}
\end{tikzpicture}

\end{document}


Result:

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

-
 Awesome that´s what I was looking for, a check for empty values before the division. Thanks a lot. – chris Nov 24 '12 at 15:22

I do not understand why you want to plot empty values. However, if you choose to use 0 instead of empty values, here is an automatic suggestion using pgfplotstable. We can replace empty cells with 0 and 0/1000 should not produce NaN errors as the result is defined (i.e., 0)

Code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{pgfplotstable}
\pgfplotstableset{col sep=semicolon}
\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},
}
]

@Harish Kumar: If you load the table first using \pgfplotstableread, the missing coordinate is plotted as 0 even without the empty cells with={0}. In fact, empty cells with has no effect on the table when it's used within a plot, it only affects table output using \pgfplotstabletypeset. – Jake Nov 23 '12 at 0:01