在这个问题,NaN
如 pgfplots 包手册中所述,字符串被省略。我的输入数据包含UnDf
,我想省略/跳过这个值。
这是我提到的问题的示例代码。如果UnDf
将 s 替换为,则可以编译此示例代码。但是,由于s 被省略,NaN
因此我需要省略UnDf
s 。NaN
\documentclass{article}
\usepackage[]{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{testdata.dat}
0 UnDf 1 1
1 1 2 UnDf
2 1.2 UnDf 2
3 2 3 1
\end{filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=1.4}
\begin{document}
\centering
\begin{tikzpicture}
\begin{axis}
\foreach \n in {1,...,3}{
\addplot table[
header=false,
x index = 0,
y index = \n]{testdata.dat};
}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
默认情况下,PGF 数学引擎仅将 识别nan
为缺失值。以下代码片段基于nan
中找到的解释 s 的代码pgfmathfloat.code.tex
,UnDf
s 也被视为缺失值:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
% Treat "UnDf" (regardless of letter case) as "nan"
\makeatletter
\expandafter\def\csname pgffltA@U\endcsname{\pgfflt@readundef}
\expandafter\def\csname pgffltA@u\endcsname{\pgfflt@readundef}
\def\pgfflt@readundef #1#2#3{%
\def\pgfflt@readnan@ok{1}%
\if#1n\else\if#1N\else\def\pgfflt@readnan@ok{0}\fi\fi
\if#2d\else\if#2D\else\def\pgfflt@readnan@ok{0}\fi\fi
\if#3f\else\if#3F\else\def\pgfflt@readnan@ok{0}\fi\fi
\if\pgfflt@readnan@ok1%
\pgfmathfloat@a@S=3\relax%
\pgfmathfloat@a@Mtok={0.0}%
\pgfmathfloat@a@E=0%
\expandafter\pgfflt@finish
\else
\def\pgfflt@readnan@{\pgfflt@error #1#2#3}%
\expandafter\pgfflt@readnan@
\fi
}
\makeatother
\begin{filecontents}{testdata.dat}
0 UnDf 1 1
1 1 2 unDF
2 1.2 UnDf 2
3 2 3 1
\end{filecontents}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\foreach \n in {1,...,3}{
\addplot table[
header=false,
x index = 0,
y index = \n]{testdata.dat};
}
\end{axis}
\end{tikzpicture}
\end{document}