0

I have a pgfplotstable that contains coordinates of points for example

\pgfplotstableread{
    t x
    0.0 0.0
    1.0 2.0
    3.0 5.0
    6.0 10.0
    11.0 12.0
}\points

I want to create another table with the slopes around the second and the fourth points. I used the following code to compute the slopes:

\foreach \j in {1,3} {% using foreach because in reality I am using a very large table
    \pgfmathsetmacro{\jb}{\j + 1}% The index of the previous point
    \pgfmathsetmacro{\ja}{\j - 1}% The index of the next point
    

    \pgfplotstablegetelem{\j}{t}\of\loadedtable% The value of t in the current point
    \pgfmathsetmacro{\t}{\pgfplotsretval}% Set the value of t in the variable \t

    \pgfplotstablegetelem{\ja}{t}\of\loadedtable% The value of t of the previous point
    \pgfmathsetmacro{\ta}{\pgfplotsretval}% Set the value in the variable \ta
    \pgfplotstablegetelem{\ja}{x}\of\loadedtable% The value of x of the previous point
    \pgfmathsetmacro{\xa}{\pgfplotsretval}% Set the value in the variable \xa
    
    % Doing the same thing for the next point
    \pgfplotstablegetelem{\jb}{t}\of\loadedtable%
    \pgfmathsetmacro{\tb}{\pgfplotsretval}%
    \pgfplotstablegetelem{\jb}{x}\of\loadedtable%
    \pgfmathsetmacro{\xb}{\pgfplotsretval}%
    
    % Computing the slope
    \pgfmathsetmacro{\v}{(\xb - \xa)/(\tb - \ta)}%
}

However it does not work very good and I don't know how to use it to create a new table with slopes. My aim is to have a way to store the values of t (time) and v (slope) in a new table. Can anyone help me to do that correctly?

1 Answer 1

1

Easy to do, hard to figure out how.

BTW, you don't really need a new table since you can select which columns you want to use/print.

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread{
    t x
    0.0 0.0
    1.0 2.0
    3.0 5.0
    6.0 10.0
    11.0 12.0
}\points

\pgfplotstableset{create on use/v/.style=
  {create col/expr={(\thisrow{x}-\prevrow{x})/(\thisrow{t}-\prevrow{t})}}}
  
\pgfplotstabletypeset[columns={t,x,v}]\points

\end{document}
2
  • Thank you. Do you know how to select only a subset of rows?
    – Kroki
    Commented Oct 29, 2020 at 20:10
  • 1
    use skip rows between index={begin}{end} (page 43). Commented Oct 29, 2020 at 21:46

You must log in to answer this question.

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