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?

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

| improve this answer | |
  • 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? – Fernando Nadal Jun 24 '11 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 Jun 25 '11 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
}
| improve this answer | |

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.