使用 pgfplotstable 获取每行的(可变)列数?

使用 pgfplotstable 获取每行的(可变)列数?

我已经看到了PGFplots 和表的列中元素数量不同;但这是一个稍微不同的问题……

考虑以下 MWE:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{adjustbox}

\pgfplotstableread[
  col sep=comma,
  header=false,
% columns/0/.style={string type}, % nowork here; on typeset
]{
c002,128,64,0.000119,-1,-1,0.000162,-1,-1,0.000494,-1,-1,0.001540,1,65,0.001906,0,1,0.002997,1,1,0.003341,0,65,0.004432,1,65,0.004797,0,1,0.005889,1,1,0.006253,0,65,
c003,128,64,0.000166,-1,-1,0.000463,-1,-1,0.001546,1,65,0.001936,0,1,0.002981,1,1,0.003362,0,65,0.004437,1,65,0.004939,0,1,0.005918,1,1,0.006269,0,65,
c004,128,64,0.000118,-1,-1,0.000161,-1,-1,0.000456,-1,-1,0.001518,1,65,0.001903,0,1,0.002973,1,1,0.003339,0,65,0.004410,1,65,0.004795,0,1,0.005866,1,1,0.006252,0,65,
}\mytable

\pgfplotsset{%
  compat=1.5.1,
}

\pgfplotstablegetrowsof{\mytable} %Determine no. of rows
\pgfmathtruncatemacro{\rows}{\pgfplotsretval} % pgfmathsetmacro float, this int
\pgfmathtruncatemacro{\lastrow}{\rows-1}


\begin{document}
\typeout{Rows: \rows} % 3.0

Test 1: \\ \newline


\foreach \r in {0,1,...,\lastrow}{
  \pgfplotstablegetelem{\r}{[index]0}\of\mytable%
  \edef\colzerotag{\pgfplotsretval}%
  \pgfplotstablegetcolsof{\mytable} % how to get cols for this row???
  \edef\numcolsstr{\pgfplotsretval}%
  \noindent \r: \colzerotag\ - \numcolsstr \\
}


% just testing wide printout
\begin{adjustbox}{width=\textwidth}
\noindent\pgfplotstabletypeset[
  columns/0/.style={string type},
  font=\tiny,
]{\mytable}
\end{adjustbox}

\end{document}

数据的列数可变;我想避免重新解析数据并用空列填充。按原样使用数据pdflatex会失败,并显示以下内容:

% ! 包 pgfplots 错误:输入表 '' 在第 '2' 行的列数不平衡(预期为 '37' 列;实际为 '34')。输入表可能已损坏?如果您需要不平衡的数据,请考虑在空单元格中使用 'nan'(可能与 'unbounded coords=jump' 结合使用)。

如果你继续按“Enter”,文档最终将被编译,这是输出(单击可查看完整尺寸):

测试.png

呈现了完整的表格,只是为了确认是否像我一样解释每行的数据 - 即“从头开始,并继续执行尽可能多的数据列”;看起来确实如此。附加了一个空列,显然是因为数据中pgfplotstable存在尾随逗号,

所以,基本上,我想要做的是循环遍历每一行,打印第一列标签作为标识符,然后打印该行的列数;预期输出是:

0: c002 - 37
1: c003 - 37
2: c004 - 34

这里只是作为一个演示,我\pgfplotstablegetcolsof{\mytable}将始终使用表中第一个推导出的列数(因此无论如何它总是打印 37)。

有没有一种方法可以仅使用pgfplotstable(可能使用额外的 Latex 包)来实现这一点,而无需重新解析数据?

相关内容