f



Assigning an array to a new struct field.

Greetings,


I am unable to add a new field to my structure array with a simple
assignment. I have tried a number of permutations (a rather large
number) with no success. I was hoping someone might be able to help
me with the syntactic sugar I so desperately need.


---
s = struct('field', [3 4 5]);
values = [4 5 6];
s.newField = values; % bad code!
---


This code does not work!


I have tried wrapping both sides in [] (or just one), I have tried
using s.newField = deal(values), but this assigns _all_ the values to
each member of struct.


Now I know that if I can convert my array [3 4 5] into a comma
separated list 3, 4, 5, it would work.


So, I have tried several different approaches, but I simply cannot
see how.


Any advice out there?


Peter van Hardenberg
Institute of Ocean Sciences
Sidney, BC, Canada
0
10/14/2003 5:52:57 PM
comp.soft-sys.matlab 211266 articles. 15 followers. lunamoonmoon (258) is leader. Post Follow

3 Replies
711 Views

Similar Articles

[PageSpeed] 10

Peter van Hardenberg wrote:
>
>
> Greetings,
>
> I am unable to add a new field to my structure array with a simple
> assignment. I have tried a number of permutations (a rather large
> number) with no success. I was hoping someone might be able to help
> me with the syntactic sugar I so desperately need.
>
> ---
> s = struct('field', [3 4 5]);
> values = [4 5 6];
> s.newField = values; % bad code!
> ---
>
> This code does not work!
>
<snip>


Your code works perfectly on my system


>> s = struct('field', [3 4 5]);
values = [4 5 6];
s.newField = values; % bad code!
>>
>> s
s =
       field: [3 4 5]
    newField: [4 5 6]


/ per


----------------------------------------------------------------------
---------------
MATLAB Version 6.5.0.180913a (R13)
Operating System: Microsoft Windows 2000 Version 5.0 (Build 2195:
Service Pack 4)
Java VM Version: Java 1.3.1_01 with Sun Microsystems Inc. Java
HotSpot(TM) Client VM
----------------------------------------------------------------------
---------------
0
not9327 (46)
10/14/2003 6:15:43 PM
"Peter van Hardenberg" <vanhardenbergP@pac.dfo-mpo.gc.ca> writes:

> I am unable to add a new field to my structure array with a simple
> assignment. I have tried a number of permutations (a rather large
> number) with no success. I was hoping someone might be able to help
> me with the syntactic sugar I so desperately need.
>
>
> ---
> s = struct('field', [3 4 5]);
> values = [4 5 6];
> s.newField = values; % bad code!

I'm assuming you mean this to be:

s = struct('field', {3 4 5});

with curly braces, so that s is a 1x3 struct array.  The admittedly
non-intuitive magic incantation seems to be:

v = num2cell(values);
[s.newField] = deal(v{:});

-- 
Peter Boettcher <boettcher@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/
0
boettcher (2304)
10/14/2003 6:19:17 PM
per isakson wrote:
<SNIP preamble about problem assigning arrays to structure
fields>


<CODE>
s = struct('field', {3 4 5});
values = [4 5 6];
s.newField = values; % bad code!
</CODE>


> Your code works perfectly on my system
>
> s =
> field: [3 4 5]
> newField: [4 5 6]
>
> / per
>


Per, I swapped my brackets there.


The result I am looking for was:


s(1):
    field: 3
    newField: 4


s(2):
    field: 4
    newField: 5


s(3):
    field: 5
    newField: 6


In order to get the expected (albeit undesired) behaviour from my
code, run it as changed (all I have done is change the struct
creation braces from [] to {}).


Now, I HAVE found a way around this. It might not be the Right Way To
Do It, but it Does It. (Having said that, if you know a Right Way,
I'd love to hear it.)


This code works as desired:
<CODE>
s = struct('field', {3 4 5});
values = [4 5 6];


%s.newField = values; % bad code!
intermediateCellArray = num2cell(values);
[s.newField] = deal(intermediateCellArray{:});
</CODE>


It seems hackish to me -- particularly the intermediate array, but in
all my matlab travels, I have never found the way to index into the
output of a function (and I despair of ever learning) so this was the
best I could do.


(I would rather, of course, say:
[s.newField] = deal(num2cell(values){:});
)


Anyway, thanks.


Peter van Hardenberg
Institute of Ocean Sciences
Sidney, BC, Canada
0
10/14/2003 6:28:37 PM
Reply: