Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have written this code and it works:

...
\DTLsetseparator{;}
\DTLloaddb[noheader]{DBcurva}{Curvas.csv}
\begin{tikzpicture}
\foreach \columna in {1,3,...,\DTLcolumncount{DBcurva}}{
\edef\listaPuntos{}
    \pgfmathtruncatemacro{\columnasig}{1+\columna} 
    \foreach \fila in {1,2,...,\DTLrowcount{DBcurva}}
    {
      \DTLgetvalue{\cX}{DBcurva}{\fila}{\columna}
      \DTLgetvalue{\cY}{DBcurva}{\fila}{\columnasig}
      \xdef\listaPuntos{(\cX,\cY) -- \listaPuntos}
    }
    \draw \listaPuntos cycle;
  }
\end{tikzpicture}
...

This reads a CSV file called "Curvas.csv". The columns are coordinates of points on curves (X1,Y1,X2,Y2,X3,Y3,X4,Y4..). The code draws all the curves in the file (initially I don't know how many there are) without problems.

Is there a better way (I can't separate each curve in different files)?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

You could use pgfplots to loop through the columns in your datafile and draw the curves:

\documentclass{minimal}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}

\begin{axis}[every plot/.style={no markers},cycle list name=linestyles]
\pgfplotstableread[col sep=comma] {testdata.csv}\data % Save the CSV to a macro
\pgfplotstablegetcolsof{\data} % Get the number of columns n
\pgfmathparse{\pgfplotsretval-2} % Calculate column index Xn = n-2
\let\xmax\pgfmathresult

\foreach \x in {0,2,...,\xmax} {
    \pgfmathadd{\x}{1} % Offset one to get Y column index
    \let\y\pgfmathresult
    \addplot+[no markers,line width=2pt] table [x index=\x,y index=\y] \data;
}
\end{axis}

\end{tikzpicture}
\end{document}

testdata.csv:

X1,Y1,X2,Y2,X3,Y3,X4,Y4
0,0,0,1,0,2,0,3
4,2,5,1,6,6,6,0
8,3,10,4,10,1,10,10

enter image description here

share|improve this answer
    
Thanks a lot, it's a lot faster !! –  Fernando Nadal Jan 27 '11 at 18:11
    
@Fernando: If this answers is what you want, please accept it by using the tick-mark symbol below the voting arrows. This way your question is removed from the "unanswered question" section and Jake gets some points as rewards. You should also up-vote correct answers if you not already did that. Thanks! –  Martin Scharrer Mar 2 '11 at 16:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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