Christina
Christina

Reputation: 935

concatenation between variable and string

I am trying to concatenate a variable with a string. For example I want to get d1, d2 and d3.

I know that to concatenate 'd' with 1 , 'd' with 2 and 'd' with 3 , it is necessary to convert 1, 2 and 3 to string. The code below work very well :

['d' num2str(1)] = 4;
['d' num2str(2)] = 5;
['d' num2str(3)] = 6; 

But when I tried the code below:

for i=1:3
['d' num2str(i)] = i+3;
end

Unfortunately I always get the error : An array for multiple LHS assignment cannot contain LEX_TS_STRING

Any help will be very appreciated.

Upvotes: 2

Views: 1437

Answers (1)

Divakar
Divakar

Reputation: 221714

Use EVALC -

for i=1:3
    evalc(['d' num2str(i) '=' num2str(i+3)]);
end

EDIT 1: If d1=3; and you need to get d2 = d1+4 and d3 = d2+4;, use this -

d1 = 3;
for i=2:3
    evalc(['d' num2str(i) '=d' num2str(i-1) '+4']);
end

Upvotes: 3

Related Questions