2

enter image description here

I am trying to plot data from .csv file. I have a lot of .csv files (around 4000). I am not sure how to do this. I am looking for some kind of "for loop" to perform this. Any help would be appreciated. I have attached an example code below.

I have data a lot of data files and I do not want to add one line of code for all .csv files. I hope this explains my problem.

%\title{Plot_for_gain_tuning_paper}
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{4pt}
\pgfplotsset{width=9.75cm,compat=1.3} 
\pgfplotsset{every axis legend/.append style={at={(0.45,-0.25)},legend columns=5,anchor=south,font=\small}}
\tikzset{every mark/.append style={scale=0.8}}
\pgfplotsset{compat=1.8}
\begin{document}

\begin{tikzpicture}
\begin{axis}[title=$V_{a_{rms}}+V_{b_{rms}}+V_{c_{rms}}$,xlabel={Time (in seconds)},ylabel={Voltage (in Volts)},grid=major,legend entries={$Index~1$,$Index~2$,$Index~3$,$Index~4$,$Index~5$}]
\addplot[color=black,style=solid,very thick] table [col sep=comma] {Data_128_4_1.csv};
\addplot[color=black,style=solid,very thick] table [col sep=comma] {Data_128_4_2.csv};
\addplot[color=black,style=solid,very thick] table [col sep=comma] {Data_128_4_3.csv};
\addplot[color=black,style=solid,very thick] table [col sep=comma] {Data_128_4_4.csv};
\end{axis}
\end{tikzpicture}
\end{document}
  • It would be helpful to know the pattern by which the files are named. Is it as straightforward as it looks: Data_128_4_1.csv, ..., Data_128_4_4000.csv? – jon Feb 21 '15 at 4:59
  • The data looks something like the following: 0,0 0.2,0 0.2,0 0.2,0 0.51962,0 0.81224,0 1.3398,0 ..... etc. The problem I am facing here is not with the csv files, but how I include this in the latex code. I have data files from "Data_128_4_1.csv" to "Data_128_4_1000.csv". I am trying to avoid writing 1000 lines of \addplot. Hope this answers the question. – user2981841 Feb 21 '15 at 5:11
  • I have uploaded a picture which shows the typical data in the .csv file. – user2981841 Feb 21 '15 at 5:13
  • I meant the filenames, not the content. – jon Feb 21 '15 at 5:23
  • The basic principle might be: \foreach \C in {1,2,...,1000} {\addplot[<options>]{Data_128_4_\C.csv};}. Hard to do more on my end without constructing a large number of .csv files to test with.... – jon Feb 21 '15 at 5:31
3

For a collection of files that has a regular 'progression' of filenames, the \foreach command (from the package pgffor, a utility package for tikz) can be helpful. Instead of

\begin{tikzpicture}
\begin{axis}[title=$V_{a_{rms}}+V_{b_{rms}}+V_{c_{rms}}$,xlabel={Time (in seconds)},ylabel={Voltage (in Volts)},grid=major,legend entries={$Index~1$,$Index~2$,$Index~3$,$Index~4$,$Index~5$}]
\addplot[color=black,style=solid,very thick] table [col sep=comma] {Data_128_4_1.csv};
\addplot[color=black,style=solid,very thick] table [col sep=comma] {Data_128_4_2.csv};
\addplot[color=black,style=solid,very thick] table [col sep=comma] {Data_128_4_3.csv};
...
\addplot[color=black,style=solid,very thick] table [col sep=comma] {Data_128_4_1000.csv};
\end{axis}
\end{tikzpicture}

you could have:

\begin{tikzpicture}
\begin{axis}[
        title=$V_{a_{rms}}+V_{b_{rms}}+V_{c_{rms}}$,
        xlabel={Time (in seconds)},
        ylabel={Voltage (in Volts)},
        grid=major,
        legend entries={$Index~1$,$Index~2$,$Index~3$,$Index~4$,$Index~5$}
    ]

    \foreach \F in {1,2,...,1000}{
            \addplot[
                color=black,
                style=solid,
                very thick
            ] table [col sep=comma] {Data_128_4_\F.csv};
        }

\end{axis}
\end{tikzpicture}

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.