2

Is it possible to determine the number of rows in a table read by pgfplotstableread{values.dat}\val ?

My setup is the following. I make experiments to see how a parameter affects a convergence curve. Results of such experiments look like

[results.dat]
n err_0 err_1 err_2 err_3
1024 0.1 0.2 0.15
2084 0.01 0.02 0.015
4096 0.001 0.002 0.0015

Parameters for each experiment is saved in a second table

[meta.dat]
0.5
2
3.7
11

Now I want to plot the results automatically (without knowing the number of experiments or the parameters)

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread{results.dat}\values
\pgfplotstableread{meta.dat}\meta
\def\N{3} %change to automatic here

\begin{tikzpicture}
\begin{loglogaxis}
\foreach \i in {0,...,\N}{
%use elements from meta table here instead of \i
\edef\theLegend{\noexpand\addlegendentry{Generalized \i}}
\addplot table[x=n,y=err_\i]{\values};
\theLegend
}   
\end{loglogaxis}
\end{tikzpicture}
\end{document}

The two points I have trouble with are marked with comments.

  • During \pgfplotstabletypeset you can access it via \pgfplotstablerows – percusse Feb 21 '14 at 13:54
  • Can I use that without typesetting it at all? I use it for plots. – papabravo Feb 21 '14 at 13:57
  • Can you make an example how you want to use it? for plots you also have \coordindex in pgfplots – percusse Feb 21 '14 at 14:01
  • I added the shortest example I could think of. – papabravo Feb 21 '14 at 14:13
  • 2
    Two things ; \pgfplotstablegetrowsof{<file name or loadedtable>} gives the number in your original question or you can loop over columns with \pgfplotstableforeachcolumn<table>. You can check which one is your case in the manual – percusse Feb 21 '14 at 14:59
3

Using the pointers of percusse and the answer https://tex.stackexchange.com/a/36472/5737 I came to the following solution for my problem.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread{results.dat}\values
\pgfplotstableread{meta.dat}\meta
%number of experiments
\pgfplotstablegetrowsof{\meta}
\pgfmathsetmacro{\N}{\pgfplotsretval-1}  

\begin{tikzpicture}
\begin{loglogaxis}
\foreach \i in {0,...,\N}{
%get parameter
\pgfplotstablegetelem{\i}{[index]0}\of{\meta} 
\let\param\pgfplotsretval
\edef\theLegend{\noexpand\addlegendentry{Generalized \param}}
\addplot table[x=n,y=err_\i]{\values};
\theLegend
}   
\end{loglogaxis}
\end{tikzpicture}
\end{document}
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.