help-octave
[Top][All Lists]
Advanced

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

Re: Updating sequential cell elements


From: Jaroslav Hajek
Subject: Re: Updating sequential cell elements
Date: Mon, 17 May 2010 09:10:03 +0200

On Mon, May 17, 2010 at 9:01 AM, AlbFrigerio
<address@hidden> wrote:
>
> Hi everyone, I've a question about cell elements.
>
> Let A=cell(1,N) be a cell and every A{i} be a structure ,i.e.
> A{i}={a,b,c,d,...} . Is it possible to initialize (or update) them all
> together , using something like A{1:N}.a = 1 ?  I tried that command but it
> gave me an error.
> I could obviously make a for loop, I'm only looking for a (probably) faster
> way.
>

If each of your structs has the same set of fields, it's better to use
a struct array:

octave:1> n = 10;
octave:2> [A(1:n).a] = deal (1)
A =
{
  1x10 struct array containing the fields:

    a
}

expressions like A.a then generate cs-lists, so use either [] or {} to
convert them to matrix or cell array:

octave:3> [A.a]
ans =

   1   1   1   1   1   1   1   1   1   1

octave:4> {A.a}
ans =

{
  [1,1] =  1
  [1,2] =  1
  [1,3] =  1
  [1,4] =  1
  [1,5] =  1
  [1,6] =  1
  [1,7] =  1
  [1,8] =  1
  [1,9] =  1
  [1,10] =  1
}

hth

-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

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