help-octave
[Top][All Lists]
Advanced

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

Re: assign struct array to cell array


From: Jaroslav Hajek
Subject: Re: assign struct array to cell array
Date: Sun, 14 Nov 2010 20:43:54 +0100

On Fri, Nov 5, 2010 at 9:48 PM, John W. Eaton <address@hidden> wrote:
> On  5-Nov-2010, Eric Chassande-Mottin wrote:
>
> |
> |
> | hi, I'd like to assign a struct array to a cell array.
> | I'm wondering whether I could do that without a loop
> | like in this example :
> |
> | clear; a=cell(3,1); b.foo=1; b(2).foo=2; b(3).foo=3; for n=1:3, a{n}=b(n);
> | endfor; a
> | a =
> |
> | {
> |   [1,1] =
> |   {
> |     foo =  1
> |   }
> |
> |   [2,1] =
> |   {
> |     foo =  2
> |   }
> |
> |   [3,1] =
> |   {
> |     foo =  3
> |   }
> | }
> |
> | all my attempts end up creating a cell array with three copies of b, e.g.:
> |
> | clear; a=cell(3,1); b.foo=1; b(2).foo=2; b(3).foo=3; [a{:}]=deal(b(:))
> |
> | I guess the reason is that b(:) isn't a cs list.
>
> You could use
>
>  a = arrayfun (@(x) x, b, "uniformoutput", false)
>
> or
>
>  a = arrayfun (@deal, b, "uniformoutput", false)
>
> jwe
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
>

a = num2cell (b);



reply via email to

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