![将回归图中 foreach 循环计算出的值保存到文件或表中](https://linux22.com/image/480708/%E5%B0%86%E5%9B%9E%E5%BD%92%E5%9B%BE%E4%B8%AD%20foreach%20%E5%BE%AA%E7%8E%AF%E8%AE%A1%E7%AE%97%E5%87%BA%E7%9A%84%E5%80%BC%E4%BF%9D%E5%AD%98%E5%88%B0%E6%96%87%E4%BB%B6%E6%88%96%E8%A1%A8%E4%B8%AD.png)
我想将这些数据集的回归斜率保存到另一张表中。我设法在 foreach 循环中的初始表中添加行,但添加图表不再起作用了……提前感谢您的建议。
\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
% data
\pgfplotstableread{
x y1 y2 y3
0 0.00 0.00 0.00
1 0.15 0.24 0.42
2 0.24 0.57 0.74
3 0.34 0.79 1.22
}\data
% slope data header
\pgfplotstableread{
N slope
}\slopedata
\begin{tikzpicture}
\begin{axis}
%loop
\pgfplotsinvokeforeach{1,...,3}{
\addplot+[only marks]
table [x=x,y index=#1]
{\data};
\addplot[no marks]
table [x=x,y={create col/linear regression={y=[index]#1}}]
{\data};
\edef\slope{\pgfplotstableregressiona}
% append new slope to slopedata
\pgfplotstableread[header=has colnames,col sep=comma,row sep=crcr]{
N,slope\\
#1,\slope\\
}\mynewrow
\pgfplotstablevertcat{\slopedata}{\mynewrow}
}
\end{axis}
\end{tikzpicture}
% Output
\pgfplotstabletypeset{\slopedata}
\end{document}