DSPRelated.com
Forums

new string+number

Started by Tengku J December 26, 2009
Dear All,
I've been receiving a lot of feed back and I would like to say thank you so much for helping me. However once I try to implement the suggested method for my real case its there's still give me an error. Hopefully u can help me with this.

Let say we already know

U1=[1 1 1 1 1];

T1=[0 1 0 0 0];

T2=[1 0 0 0 0];

T3=[0 0 0 0 1];

Now the problem should be

U2 = U1-T1; and U3 = U2-T2;
(p/s: there is no spaces between U or T with the number)
So I intend to write

for a=2:3

['U' num2str(a)] = ['U' num2str(a-1)] - ['T' num2str(a-1)]

end

I was hoping I get the answer where U2 = [1 0 1 1 1] and U3 = [1 0 1 1 0] but instead I receive an error.

??? Error: An array for multiple LHS assignment cannot contain character string.

Can you please help me? Thank you so much man.

Regards

suli
Suli,

If you enter this into matlab (as you mentioned),

for a=2:3
['U' num2str(a)] = ['U' num2str(a-1)] - ['T' num2str(a-1)]
end

Matlab does not consider ['U' num2str(a)] to be a single variable name,
I think that instead, Matlab thinks that ['U' num2str(a)] is an array that contains two new variables to be defined, 1) 'U' and 2) a number converted to a string.

Thus, the way to solve the problem would be using the eval command and concatenating the strings as:

for a=2:3
eval(['U',num2str(a),' = U',num2str(a-1),' - T', num2str(a-1)])
end

Dear All,
>I've been receiving a lot of feed back and I would like to say thank you so much for helping me. However once I try to implement the suggested method for my real case its there's still give me an error. Hopefully u can help me with this.
>
>Let say we already know
>
>U1=[1 1 1 1 1];
>
>T1=[0 1 0 0 0];
>
>T2=[1 0 0 0 0];
>
>T3=[0 0 0 0 1];
>
>Now the problem should be
>
>U2 = U1-T1; and U3 = U2-T2;
>(p/s: there is no spaces between U or T with the number)
>So I intend to write
>
>for a=2:3
>
>['U' num2str(a)] = ['U' num2str(a-1)] - ['T' num2str(a-1)]
>
>end
>
>I was hoping I get the answer where U2 = [1 0 1 1 1] and U3 = [1 0 1 1 0] but instead I receive an error.
>
>??? Error: An array for multiple LHS assignment cannot contain character string.
>
>Can you please help me? Thank you so much man.
>
>Regards
>
>suli
>