# Math Help - Matlab-change of variable name inside a loop

1. ## Matlab-change of variable name inside a loop

for i=1: 5
x(i)=2*i^2;
end

the above program yields x as a matrix.
But I want the variable name to change for every i. i.e., the 2*1^2 should be assigned to x1, 2*2^2 should be assigned to x2 e.t.c . In other words, inside the loop i should concatenates with x to form a new variable where the value of 2*i^2 will be stored.

2. ## Re: Matlab-change of variable name inside a loop

Code:
  for idx=1:5
vv=['x' , num2str(idx)];
assignin('base' , vv , 2*idx^2);
end
or:
Code:
  for idx=1:5
str=['x',num2str(idx),'=2*idx^2'];
eval(str);
end
.