How to Dynamically Create Variables in MATLAB -
i manipulate variables names. (just in php use ${}
create dynamic variables). exemple:
i want create n variables a_n name:
for = 1:n a_i = 'new variable!'; end
the result be:
a_1 a_2 ... a_n
if want create variables programmatically (not recommended practice, if insist), see previous question: a way dynamically create variables in matlab?.
as say, it's messy that, , @ least may want have things stored within structure, allows programmatic creation of elements using string variables , dynamic referencing using paren syntax. instance, in example, use:
n = 5; = 1:n fieldname = sprintf('a_%i', i); s.(fieldname) = 'new variable!'; end
if display structure s
, see:
>> s s = a_1: 'new variable!' a_2: 'new variable!' a_3: 'new variable!' a_4: 'new variable!' a_5: 'new variable!'