2

Consider this MWE:

enter image description here

\documentclass[a4paper,12pt]{article}
\usepackage{pgfplots,filecontents}

\begin{filecontents*}{data.csv}
"Amplitude","notes: data set 1",
X,Y,
1,1,
2,2,
3,3,
4,4,

"CH1","notes: data set 1",
\end{filecontents*}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}
        \addplot gnuplot [raw gnuplot, mark=none, black]{
            set datafile separator comma;
            plot "data.csv" using 1:2 every ::3::6 with lines;
        };
        \end{axis}
    \end{tikzpicture}
\end{document}

where I've used gnuplot to plot data from a .csv file. Regarding the format of my .csv file, I know the data always starts on line 3, however I don't know how long the data will be. When dealing with 1 set of data like in this example, it's not too difficult to just count that there are 4 lines and put this in manually - every ::3::6 with lines (i.e. from line 3 to line 6).

My issue is that my real .csv file is a lot more complicated - it includes multiple data blocks and they all have different lengths (which I don't know exactly how long without manually checking, on the order of 2000+ points). This extension of my MWE illustrates the problem:

\documentclass[a4paper,12pt]{article}
\usepackage{pgfplots,filecontents}

\begin{filecontents*}{data.csv}
"Amplitude","notes: data set 1",
X,Y,
1,1,
2,2,
3,3,
4,4,

"Amplitude","notes: data set 2",
X,Y,
1,7,
2,6,
3,5,
4,4,
5,3,
6,2,
7,1,

"CH1","notes: data set 1",
"CH1","notes: data set 2",
\end{filecontents*}

\begin{document}
    \begin{tikzpicture}
        \begin{axis} [width=0.5\textwidth,height=7cm,
        ]
        \addplot gnuplot [raw gnuplot, mark=none, black]{
            set datafile separator comma;
            plot "data.csv" using 1:2 every ::3::6 with lines;
        };
        \end{axis}
    \end{tikzpicture}
\end{document}

Is there a way to automate how this .csv file is processed? Ideally, I'd like to make this so TeX can work out how many data blocks are stored in the .csv and then I can select which data blocks to plot (some on individual plots, some together).

FYI, the purpose of this to enable automatic generation of reports. I select the data file, and choose the blocks I want to plot and hit run and the document automatically generates.

1

This is not a complete answer, as I don't know how to use pgfplots and gnuplot. However, this answer shows how to use readarray (and its underlying listofitems) to extract a piece of an input file.

You just now have to figure out how to pass that extracted piece to gnuplot.

\documentclass[a4paper,12pt]{article}
\usepackage{pgfplots,filecontents,readarray}

\begin{filecontents*}{mydata.csv}
"Amplitude","notes: data set 1",
X,Y,
1,1,
2,2,
3,3,
4,4,

"Amplitude","notes: data set 2",
X,Y,
1,7,
2,6,
3,5,
4,4,
5,3,
6,2,
7,1,

"CH1","notes: data set 1",
"CH1","notes: data set 2",
\end{filecontents*}
\begin{document}
\readarraysepchar{\\}
\readdef{mydata.csv}\mydata
\ignoreemptyitems
\setsepchar{%
    "Amplitude","notes: data set 2",% START OF DATA SET
  /%
    "CH1","notes: data set 1",% END OF DATA SET
  /%
    \\% SAME UNIQUE TOKEN STRING SET IN \readarraysepchar
}
\readlist\myarray{\mydata}
\noindent\foreachitem\x\in\myarray[2,1]{\xcnt: \x\\}
\end{document}

enter image description here

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.