DSPRelated.com
Forums

string+number

Started by Tengku J December 24, 2009
Dear All,

I wish to write

Cat1 = 4
Cat2 = 5
Cat3 = 6

(p/s: there are no space between 'cat' and numbers)

However I want to do it with "for" command.

For example;
for i=1:3
Cati=i+3;
end

And the answer will be

Cat1 = 4

Is there any way do it?

Really appreciate your helps. Thank you.

-suli-
Hi,

You can do this
for i=1:3
eval(['cat',num2str(i),'=i+3;']);
end

But, this will drastically slow down your execution time and makes code
readability more harder. I strongly discourage such coding style.

Cheers,
Tilak

On Thu, Dec 24, 2009 at 8:22 AM, Tengku J wrote:

> Dear All,
>
> I wish to write
>
> Cat1 = 4
> Cat2 = 5
> Cat3 = 6
>
> (p/s: there are no space between 'cat' and numbers)
>
> However I want to do it with "for" command.
>
> For example;
> for i=1:3
> Cati=i+3;
> end
>
> And the answer will be
>
> Cat1 = 4
>
> Is there any way do it?
>
> Really appreciate your helps. Thank you.
>
> -suli-
>
>
>
Hello,

Yes, I know a way for it:

for i = 1:3
eval(['cat' num2str(i) ' = i+3'])
end

the best...

Baran
2009/12/24 Tengku J

> Dear All,
>
> I wish to write
>
> Cat1 = 4
> Cat2 = 5
> Cat3 = 6
>
> (p/s: there are no space between 'cat' and numbers)
>
> However I want to do it with "for" command.
>
> For example;
> for i=1:3
> Cati=i+3;
> end
>
> And the answer will be
>
> Cat1 = 4
>
> Is there any way do it?
>
> Really appreciate your helps. Thank you.
>
> -suli-
>
>
>

--
Baran USLU, M.Sc.
Ogr.Gor.
Baskent Universitesi
Elektrik-Elektronik Muhendisligi Bolumu

Tel. 0312-234 1010/ 1221
Faks: 0312-234 1051
www.baskent.edu.tr/~ibuslu