Basically the problem was about generating this kind of array {<num1>, <num2>, <num3>, ...}. It occurred to be quite tricky, but I managed to find the solution.
The key to solve the problem is an expansion. \xdef(equivalent to \global\edef) allows to define an expanded macro so that I it can access itself in its definition. For example
\edef\foo{-3} % -> -3
\edef\foo{\foo,6} % -> -3,6
First solution based on \push macros that I found in this post. And the following code forms the desired array
\def\push#1#2{\expandafter\xdef\csname #1\endcsname{\expandafter\ifx\csname #1\endcsname\empty\else\csname #1\endcsname,\fi#2}}
\def\xarr{}
\def\yarr{}
\foreach \i [evaluate={\x=\points[\i][0]; \y=\points[\i][1]}] in {0,...,\len}{
\push{xarr}{\x}
\push{yarr}{\y}
}
Second solution doesn't require that macros, but still based on expanded definition
\def\xarr{}
\def\yarr{}
\foreach \i [evaluate={\x=\points[\i][0]; \y=\points[\i][1]; \c=ifthenelse(\i<\len,",",)}] in {0,...,\len}{
\xdef\xarr{\xarr\x\c}
\xdef\yarr{\yarr\y\c}
}
MWE
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\def\points{{{-9,0.30},{-6,0.44},{-2,0.59},{3,1}}}
\pgfmathsetmacro{\len}{dim(\points)-1}
\def\push#1#2{\expandafter\xdef\csname #1\endcsname{\expandafter\ifx\csname #1\endcsname\empty\else\csname #1\endcsname,\fi#2}}
\def\xarr{}
\def\yarr{}
% First method
%\foreach \i [evaluate={\x=\points[\i][0]; \y=\points[\i][1]}] in {0,...,\len}{
% \push{xarr}{\x}
% \push{yarr}{\y}
%}
% Second method
\foreach \i [evaluate={\x=\points[\i][0]; \y=\points[\i][1]; \c=ifthenelse(\i<\len,",",)}] in {0,...,\len}{
\xdef\xarr{\xarr\x\c}
\xdef\yarr{\yarr\y\c}
}
\begin{axis}
[
axis lines = center,
axis line style = thick,
% xtick= loop here for \points[\i][0]
% ytick= loop here for \points[\i][1] where \i=0,...,\len
clip=false
]
\addplot ({x},{x^2});
\end{axis}
\end{tikzpicture}
\end{document}
Note that if you have babel package added, the second method might throw an error. That's all because of " inside ifthenelse. In order to fix that, use ifthenelse(\i<\len,\string",\string",), or use \shorthandoff{"}.
\pointsarray – antshar Nov 07 '21 at 11:06axisI don't see a reason to give up the initial array. I still don't know a way of converting this one into mine that can be iterated with an access to a specific index. So\foreach \i [evaluate={\ix=\points[\i][0]; \iy=\points[\i][1];}] in {0,...,\len}{ \addplot [only marks, opacity=0] coordinates {(\ix,0) (0,\iy)}; }will do. – antshar Nov 08 '21 at 15:24{{{x1,y1},{x2,x2},...}}format, but yours is{(x1,y1),(x2,y2),...}– antshar Nov 08 '21 at 15:44