Gopalpur
Gopalpur

Reputation: 121

Change a name and assign matrix in matlab

I wish to change the name of col_1 to col_2, col_3....col_N for each value of 'j'. Could anyone suggest on how to handle this.? The reason why I wish to do this way is that size of col_i changes for different j. Any valuable suggestions and corrections are highly appreciated.

for j=1:N       
    for i=1:dum+1
        col_1(i,1)=x;
        col_1(i,2)=y;
    end
end

Upvotes: 0

Views: 107

Answers (2)

Siva Srinivas Kolukula
Siva Srinivas Kolukula

Reputation: 1251

@KGV dynamic naming convention is not suggested in MATLAB. You have the indices already, you can call them easily. Don't try to rename/ use dynamic naming convention. You may read the below link for further information.

https://in.mathworks.com/matlabcentral/answers/105936-how-to-make-dynamic-variable-names-a1-a2-an-with-for-loop-using-eval-num2str

Upvotes: 1

OmG
OmG

Reputation: 18838

you can use eval like the following:

   for j=1:N       
       for i=1:dum+1
          eval(strcat(strcat('col_',num2str(j)),'(i,1)=x'));
          eval(strcat(strcat('col_',num2str(j)),'(i,2)=y'));
       end
   end

Upvotes: 2

Related Questions