Missing } error from multiple “\immedite\write”s within a foreach loop; the current plot has no coordinates error with gnuplot

I suspect that these errors are coming from a section of code in which I loop through a master data file and restructure it so that I can give the newly made file as an argument to pgfplots. If I surround this section with \iffalse...\fi the "missing }" error goes away, leaving me with the gnuplot warning: "the current plot has no coordinates", despite the pgfplots---which uses the same datafile (made using the likely offending foreach loop)---rendering fine. I tried placing an extra } after the \end{axis}, which does not fix the error, and placing one immediately before it, which produces an "extra }" error.

\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 Answer

I discovered the issue. I don't know why it was causing the error it was, but it was simply an issue of not closing the write stream used to write to the file holding the restructured data. Then the error passed on to the gnuplot, which failed to read any points, and then passed onto the pgfplots part and somehow manifested itself as a missing curly bracket.