8

Like I said in the title, I struggle with the import of data. The experimental data, that I get, is in a .txt file and has the following structure:

xxxxxxxxxxx

yyyyyyyyyyy

zzzzzzzzzzz

N   a   b   c    
1 3 4 5       
2 6 7 8        
3 9 8 7        
4 6 5 4   

xxxxxxxxxxx

yyyyyyyyyyy

zzzzzzzzzzz   

x,y and z is text that is not used in the plot, so I want to skip the lines on the top and bottom of the file - only the table is important. Right now I have to remove those lines by hand to use the code below

\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[]{pgfplots}
\usepackage[]{tikz}

\pgfplotsset{compat=1.9}
\pgfplotsset{every axis label/.append style={font=\large}}
\pgfplotsset{every tick label/.append style={font=\large}}
\begin{document}
    \begin{figure}
    \begin{tikzpicture}
        \begin{axis}[]
\addplot table [y=c,x=b]{mydata.txt};
        \end{axis}
    \end{tikzpicture}
\end{document}

Something else I would like to know is: can i perform a simple calculation? Because I want to plot the negative values of the row "b" from mydata.txt

For example like this:

....

\addplot table [y=c,x=g]{mydata.txt};

with g=b*(-1)

....

1
  • 3
    Welcome to TeX.SX! For the "something else" see x expr/y expr, e.g. \addplot table[y=c,x expr=-\thisrow{b}] {mydata.txt}. Skipping the first few lines is simple enough, search the pgfplots manual for skip first n, I think something like \addplot table[x=a,y=b,skip first n=10]{mydata.txt}; would skip the first 10 lines, for example. (Untested, never used that key myself.) Don't know how to skip the last part of the file though. Oct 16, 2016 at 15:10

1 Answer 1

2

As Torbjørn T. already mentioned in his comment you can use x expr and y expr to do some mathematical stuff with your table values.

He also already mentioned one method where starting rows are ignored which is skip first n. Another method would be to assign a comment character to the lines before the actual table start, which will also work for the rows after the table. But I don't know of any key to ignore non-table lines.

    \begin{filecontents}{testOne.txt}
        I am a header line
        without a comment character

        a   b
        1   2
        2   3
    \end{filecontents}
    \begin{filecontents}{testTwo.txt}
        % I am a header line
        % with a comment character
        %
        a   b
        2   4
        3   5
    \end{filecontents}
    \begin{filecontents}{testThree.txt}
        a   b
        3   6
        4   7
        %
        % I am a bottom line
        % with a comment character
    \end{filecontents}
    \begin{filecontents}{testFour.txt}
        a   b
        3   6
        4   7

        I am a bottom line
        without a comment character
    \end{filecontents}
\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            % these three work fine
            \addplot table [skip first n=3] {testOne.txt};
            \addplot table [x=a,y expr=-\thisrow{b}] {testTwo.txt};
            \addplot table {testThree.txt};

%            % this will lead to an error, because PGFPlots first tries to read
%            % the table before skipping the given indices
%            \addplot table [skip coords between index={2}{4}] {testFour.txt};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .