Automatic defining global variables in MATLAB

158 Views Asked by At

How could I define a set of variables automatically in MATLAB. For example: kt1 to kt_n*(2*n+1)

global kt1 kt2 kt3 kt4 kt5 kt6 kt7 kt8 kt9 kt10 n=2; for i=1:n*(2*n+1) eval(['kt',int2str(i),'=(i+i^2)/2']) end

I would like to global kt1:kt_n*(2*n+1) automatically, instead of first line. I wonder if someone could help me. Thank you.

1

There are 1 best solutions below

0
On BEST ANSWER

In order to create global variables kt1, ..., ktN (for some $N$), here is what you could do :

for i=1:N
  eval(['global kt' num2str(i) ';']);
end