help-octave
[Top][All Lists]
Advanced

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

assign struct array to cell array


From: John W. Eaton
Subject: assign struct array to cell array
Date: Fri, 5 Nov 2010 16:48:01 -0400

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


reply via email to

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