4

I've already seen PGFplots and table with different number of elements in columns; but this is a slightly different question...

Consider the following 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}

The data has variable number of columns; I would like to avoid re-parsing the data and padding with null columns. Using the data verbatim as is, pdflatex fails with:

% ! Package pgfplots Error: input table '' has an unbalanced number of columns in row '2' (expected '37' cols; got '34'). Maybe the input table is corrupted? If you need unbalanced data, consider using 'nan' in empty cells (perhaps combined with 'unbounded coords=jump').

If you keep on pressing "Enter", the document will eventually compile, and this is the output (click for full size):

test.png

(The full table is rendered, just to confirm whether pgfplotstable interprets the data per row as I do - that is, "start from beginning, and keep going for as many data columns are available"; and it seems that is the case. There is an empty column appended, apparently because of existence of trailing comma , in the data)

So, basically, what I'd like to do is loop through each row, print the first column label as identifier, then print the number of columns in that row; the expected output would be:

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

Here just as a demo I use \pgfplotstablegetcolsof{\mytable} which will always use the first deduced number of columns for the table (so it always prints 37 regardless).

Is there a way I could achieve this only with pgfplotstable (possibly with an extra Latex package), without having to reparse the data?

  • This MWE is very confusing.... ~34 columns have nothing to do with the intrinsic problem.... Such excessive cell contents have nothing to do with the intrinsic problem.... Now you know the probably reasons, why you get not any answer since May. a) Could you break down your table to a few columns with simple entries? b) Could you make your MWE compilable? – cis Oct 27 '18 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.