7

I need to add a custom row of data to a \pgfplotstable for drawing purposes. The commented line is the needed data. I can read and manipulate any element from table but I can't add any row. Any advice ?

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\pgfplotstableread{
    s       f
    0.0     75.9638
    0.380665    206.565
    0.58711     243.435
    0.793555    333.435
%    1.0     Element(1,2) + 360.0 = 75.9638 + 360.0
}\data

\end{document}

1 Answer 1

10

You can add new rows to a pgfplotstable using \pgfplotstablevertcat, which takes two tables as arguments and concatenates the columns that are found in both tables:

\documentclass{article}

\usepackage{pgfplotstable}

\begin{document}

% Original data
\pgfplotstableread{
    0.0     75.9638
    0.380665    206.565
    0.58711     243.435
    0.793555    333.435
}\data

% Get column names
\pgfplotstablegetcolumnnamebyindex{0}\of{\data}\to{\firstcolumnname}
\pgfplotstablegetcolumnnamebyindex{1}\of{\data}\to{\secondcolumnname}

% Retrieve desired element
\pgfplotstablegetelem{1}{[index]1}\of\data

% Perform calculation, save to \result
\pgfmathsetmacro\result{\pgfplotsretval + 360}

% Assemble new line
\edef\createsumrow{\noexpand\pgfplotstableread[header=has colnames,col sep=comma,row sep=crcr]{
    \firstcolumnname,\secondcolumnname\noexpand\\
    1.0,\result\noexpand\\
}\sum}
\createsumrow

% Concatenate
\pgfplotstablevertcat{\data}{\sum}

% Output
\pgfplotstabletypeset{\data}

\end{document}
2
  • Great, but my original data comes from files with different column names so when Assemble new line, I can't get the names of them. Jun 5, 2011 at 10:17
  • @Fernando: I've changed the code to work without knowing the column names (and it will also work if there are no column names at all). Sorry this took so long.
    – Jake
    Jun 9, 2011 at 21:36

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.