Another way: using the structure to store the loop index in the name field:
for ii=1:bar foo.(["var" num2str(ii)]) = quux; end
This creates a structure with fields like foo.var1 , foo.var1 , etc. This does what you want without using eval .
source share