MATLAB: Generate Variable Names with a Loop

concatenateloop

I have a simple question that I've been unable to find an answer for. I'd like to make n variable of the format:
variable_1 variable_2 variable_… variable_n
I know I should be using a for loop, but I cant figure out how to concatenate the string with the counter.
Thanks

Best Answer

N = 10; % number of variables
%method 1
for k=1:N
temp_var = strcat( 'variable_',num2str(k) );
eval(sprintf('%s = %g',temp_var,k*2));
end
% method 2, more advisable
for k=1:N
my_field = strcat('v',num2str(k));
variable.(my_field) = k*2;
end