6

I have a \pgfplotstable with data (loaded from a file) like this one:

\pgfplotstableread{
  s       f
  0.0     95.9638
  0.2380665    160.565
  0.511     190.435
  0.705    270.435
  1.0         455.9638
}\data

I need to generate this \pgfplotstable rearranging rows and columns of \data:

\pgfplotstableread{
  s       f
  1.0         270.435
  0.705    190.435
  0.511     160.565
  0.2380665    95.9638
  0.0     <95.9638 or any other value>
}\customdata

The rows are in inverted order and the second column displaced 1 element above.

Any advice, please?

2 Answers 2

5

You can do this a bit simpler by using inbuilt methods to reverse the order and create a new column that contains the shifted values:

\documentclass{article}   
\usepackage{pgfplotstable}
\usepackage{pgfplots}   
\begin{document}
\pgfplotstableread{
  s       f
  0.0     95.9638
  0.2380665    160.565
  0.511     190.435
  0.705    270.435
  1.0         455.9638
}\originaldata

\pgfplotstablegetcolumnnamebyindex{1}\of{\originaldata}\to{\secondcolumnname}

\pgfplotstablesort[sort cmp=float >]{\data}{\originaldata}

\pgfplotstableset{
    create on use/shifted/.style={
    create col/expr={\nextrow{\secondcolumnname}*1}}
}

\pgfplotstabletypeset{\originaldata}\hspace*{1cm}
\pgfplotstabletypeset[columns={[index]0,shifted},
    columns/shifted/.style={column name=\secondcolumnname}]\data

\begin{tikzpicture}
\begin{axis}
\addplot table {\originaldata};
\addplot table [y=shifted] {\data};
\end{axis}
\end{tikzpicture}
\end{document}

plot of original and shifted data

2
  • Thanks, but I need the new table for drawing porpouses: \addplot[..] table \customdata. Is there any way to create the new table instead of \pgfplotstabletypeset? Commented Jun 24, 2011 at 21:47
  • @Fernando: You don't need to save the table in a new macro, you can just call it directly in pgfplots. I've edited my answer to show this.
    – Jake
    Commented Jun 25, 2011 at 4:05
2

I did it!

\pgfplotstablegetcolumnnamebyindex{0}\of{\data}\to{\firstcolumnname}
\pgfplotstablegetcolumnnamebyindex{1}\of{\data}\to{\secondcolumnname}

\pgfplotstablegetrowsof{\data}
\pgfmathsetmacro\ymax{\pgfmathresult - 1}

\pgfplotstablevertcat{\customdata}{\data};

\pgfplotstablemodifyeachcolumnelement\firstcolumnname\of\customdata\as\cell{
  \pgfmathsetmacro\newrow{\ymax - \pgfplotstablerow}
  \pgfplotstablegetelem{\newrow}{\firstcolumnname}\of\data
  \edef\cell{\pgfplotsretval}
}

\pgfplotstablemodifyeachcolumnelement\secondcolumnname\of\customdata\as\cell{
  \ifnum \pgfplotstablerow < \ymax
    \pgfmathsetmacro\filaant{\ymax - \pgfplotstablerow - 1}
    \pgfplotstablegetelem{\newrow}{\secondcolumnname}\of\data
    \edef\cell{\pgfplotsretval}
  \fi
}

You must log in to answer this question.

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