help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: append numbers to variable


From: Gerald Ebberink
Subject: Re: append numbers to variable
Date: Thu, 07 Sep 2006 11:39:35 +0200

On Thu, 2006-09-07 at 11:07 +0200, Carlo de Falco wrote:
> On 9/7/06, Gerald Ebberink <address@hidden> wrote:
> > something like this:
> > x=0;
> > for i=1:5
> >         command=sprintf("x%d=%d",i,i);
> >         eval(command);
> > endfor
> >
> 
> This works, but I don't understand why you would prefer this over:
> 
> >>x0 = 0;
> >>x   = x0 + [1:5];
> >>x(1)
> ans = 1
> >>x(2)
> ans = 2
> >>x(3)
> ans = 3
> >>x(4)
> ans = 4
> >>x(5)
> ans = 5
> 
> i.e. why would you create a set of variables with different names instead
> of storing your values in a vector?
> It is much more convenient to be able to access one of the values via
> 
> >>x(i)
> rather than
> >>eval(sprintf("x%d",i))

I personaly us that option a lot. Except when it comes to filenames and
the variables contained in it. I try to keep the variable and the
filename the same, so sometimes you get these "weird" constructions.

Kind regards,
Gerald Ebberink


-- 
NCLR B.V.

Visiting address                                Postal address
Drienerlolaan 5                                 PO box 2662 
7522 NB Enschede                                7500 CR Enschede
Netherlands                                     Netherlands

Phone : (+31) (0)53-489 11 10 - Fax : (+31) (0)53-489 11 02
Direct line (+31) (0)53-489 39 61





reply via email to

[Prev in Thread] Current Thread [Next in Thread]