foreach 迴圈中多個「\immedite\write」缺少 } 錯誤;目前繪圖沒有 gnuplot 座標錯誤

foreach 迴圈中多個「\immedite\write」缺少 } 錯誤;目前繪圖沒有 gnuplot 座標錯誤

我懷疑這些錯誤來自一段程式碼,在該程式碼段中我循環遍歷主資料檔案並重組它,以便我可以將新建立的檔案作為 pgfplots 的參數。如果我用“missing }”包圍這個部分,\iffalse...\fi錯誤就會消失,留下 gnuplot 警告:“當前圖沒有坐標”,儘管 pgfplots 使用相同的數據文件(使用可能有問題的 foreach 循環製作) )---渲染良好。我嘗試}在 後面放置一個額外的字符\end{axis},這不會修復錯誤,並在它之前放置一個額外的字符,這會產生“額外的}”錯誤。

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage{filecontents}
\usepackage{indentfirst}
\usepackage{amsmath}
\usepackage{float}
\usepackage{hhline}
\usepackage{multirow}
\usepackage{datatool}
\usepackage{booktabs}
\usepackage[strict]{changepage}
\usepackage[position=top]{subfig}

\begin{filecontents}{data.dat}
28.7 28.4 43.4
0.2416 0.2415 nan
0.2420 0.2416 nan
0.2421 0.2416 nan
0.2416 0.2422 nan
0.2410 0.2422 nan
35.9 35.6 50.4
0.2695 0.2691 nan
0.2679 0.2691 nan
0.2691 0.2697 nan
0.2691 0.2691 nan
0.2686 0.2694 nan
42.6 42.3 57.4
0.2951 0.2953 nan
0.2950 0.2951 nan
0.2952 0.2956 nan
0.2951 0.2957 nan
0.2951 0.2954 nan
47.2 46.9 61.9
0.3091 0.3104 nan
0.3095 0.3107 nan
0.3106 0.3107 nan
0.3105 0.3110 nan
0.3107 0.3103 nan
51.3 51.1 66.1
0.3234 0.3231 nan
0.3231 0.3235 nan
0.3238 0.3233 nan
0.3229 0.3241 nan
0.3243 0.3230 nan
57.4 57.2 72.3
0.3417 0.3431 nan
0.3426 0.3431 nan
0.3422 0.3437 nan
0.3430 0.3427 nan
0.3429 0.3437 nan
\end{filecontents}

\pgfplotsset{compat=1.14}
\pgfplotstableread{data.dat}\Data

\begin{document}
\newwrite\ostreama
\immediate\openout\ostreama=table1.dat
\foreach\row in {0,...,35}
{%
        \newcount\counttable
        \counttable=\row
        \divide\counttable by 6
        \multiply\counttable by 6
        \ifnum\row=\counttable
                %
        \else
                \pgfplotstablegetelem{\row}{0}\of\Data \pgfmathsetmacro\yval{\pgfplotsretval}%
                \pgfmathsetmacro\ysval{\yval ^ 2}%
                \pgfplotstablegetelem{\counttable}{0}\of\Data \pgfmathsetmacro\xval{\pgfplotsretval}%
                \immediate\write\ostreama{\xval\space\ysval}%
        \fi%
}

\begin{tikzpicture}
\begin{axis}[
    title style = {align=center},
        title = {Distance Fallen Plotted Against Time to Fall for Steel Ball},
        axis lines = left,
        xlabel = {distance in centimeters, $y$},
        ylabel = {time squared in seconds squared, $t^2$},
        domain = {20:60},
        xmin = 20, xmax = 60,
        ymin = 0.05, ymax = 0.12,
        xtick = {20, 25, 30, 35, 40, 45, 50, 55, 60},
        ytick = {0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12},
        xmajorgrids = true,
        ymajorgrids = true,
        legend pos = outer north east,
]
\addplot[only marks, mark = *] file {table1.dat};
\addlegendentry{$(y, t^2)$}

\addplot+[raw gnuplot, mark = none, thick, blue] gnuplot {
        f(x)=a*x+b;
        a = 1;
    b = 1;
    set fit errorvariables;
                fit f(x) 'table1.dat' using 1:2 via a, b;
                plot [x=20:60] f(x);
        set print "pars1.dat";
        print a, a_err;
        print b, b_err;
};
\addlegendentry{\pgfplotstableread{pars1.dat}\parameters
        \pgfplotstablegetelem{0}{0}\of\parameters \pgfmathsetmacro\paramA{\pgfplotsretval}
        \pgfplotstablegetelem{1}{0}\of\parameters \pgfmathsetmacro\paramB{\pgfplotsretval}
        $\Delta t^2 = \pgfmathprintnumber{\paramA}y + \pgfmathprintnumber{\paramB}$
};
\end{axis}
\end{tikzpicture}

\end{document}

答案1

我發現了這個問題。我不知道為什麼它會導致錯誤,但這只是一個不關閉用於寫入保存重組資料的檔案的寫入流的問題。然後錯誤傳遞到 gnuplot,它無法讀取任何點,然後傳遞到 pgfplots 部分並以某種方式表現為缺少大括號。

相關內容