Programmatically setting column style of a matrix in TikZ
You can expand the variables by using an \edef
to define a new command:
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\tikzstyle{mymatrix}=[draw]
\foreach \c [count=\i] in {red, green, blue} {
\globaldefs=1
\edef\dotikzset{
\noexpand\tikzset{
mymatrix/.append style={
column \i/.style={
nodes={fill=\c}
}
}
}
}
\dotikzset
}
\matrix (m) [matrix of nodes, mymatrix] {
1 & 2 & 3 & 4 \\
5 & 6 & 8 & 8 \\ };
\end{tikzpicture}
\end{document}
Jake was faster but here is something I learned from egreg in this question :
\documentclass{article}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{matrix}
\let\mystyle\empty
\newcommand{\populatestyle}{%
\foreach \c [count=\i] in {red, green, blue} {
\begingroup\edef\x{\endgroup
\noexpand\gappto\noexpand\mystyle{column \i/.style={nodes={fill=\c}},}}\x
}%
}
\populatestyle
\tikzset{mymatrix/.estyle={\mystyle}}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes,mymatrix] {
1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8 \\ };
\end{tikzpicture}
\end{document}