Thread Subject: problem concatenating structs

Subject: problem concatenating structs

From: Lars Barring

Date: 24 Nov, 2008 23:18:02

Message: 1 of 3

Hi,
I have some problems concatenating structs.
Here is a minimal example

% fill out a struct
a.a=1;
a.b=2;
a.c=3;
a(1).d=4;
% copy this, and add another field
b=a;
b.e=5;
% copy the first one (don't want to destroy it...)
c=a;
c(1)=b; % no no did not work, results in:
??? Subscripted assignment between dissimilar structures.

c=b; % did work
% to me this is somewhat strange,
% especially in the light that the following works

a(2).f=6: % works fine:

a =
1x2 struct array with fields:
    a
    b
    c
    d
    f

a(1)
ans =
    a: 1
    b: 2
    c: 3
    d: 4
    f: []

> a(2)
ans =
    a: []
    b: []
    c: []
    d: []
    f: 6

In reality I am using Jos' excellent FEX contribution CATSTRUCT:

D=C(k);
D.newfield=xyz;
%etc
C(k)=catstruct(C(k),D); % fails

CATSTRUCT works as expected but because there is an index to C the line fails.

Any ideas how to solve this?

Lars

Subject: problem concatenating structs

From: Walter Roberson

Date: 25 Nov, 2008 00:52:01

Message: 2 of 3

Lars Barring wrote:

> I have some problems concatenating structs.
> Here is a minimal example

You cannot concatenate structs unless they have exactly the same set of fields
in exactly the same order. Unless Mathworks changes this in some future version,
you will have to find some other way of doing things.

> In reality I am using Jos' excellent FEX contribution CATSTRUCT:
 
> D=C(k);
> D.newfield=xyz;
> %etc
> C(k)=catstruct(C(k),D); % fails
 
> CATSTRUCT works as expected but because there is an index to C the line fails.
 
> Any ideas how to solve this?

If you want dissimilar structures, use cell arrays. Otherwise, write a small
routine:

FN = setdiff(fieldnames(C),fieldnames(D));
for FI = 1 : length(FN); C(1).(FN) = repmat(D.(FN),0,0); end

C(k) = catstruct(C(k), D);

The repmat of size 0 0 is to get the right class in place; if you know that you
are going to be using catstruct immediately afterwards, you should probably be able
to use [] (the empty array) instead of the repmat() call.

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: problem concatenating structs

From: Lars Barring

Date: 25 Nov, 2008 08:43:02

Message: 3 of 3

Walter Roberson <roberson@hushmail.com> wrote

> Lars Barring wrote:
8<<<<SNIP
> You cannot concatenate structs unless they have exactly the same set of fields
> in exactly the same order. Unless Mathworks changes this in some future version,
> you will have to find some other way of doing things.

Well, hopefully they do, because the minimal example points at some less than
obvious (to me) behaviour :-)


> If you want dissimilar structures, use cell arrays. Otherwise, write a small
> routine:
>
> FN = setdiff(fieldnames(C),fieldnames(D));
> for FI = 1 : length(FN); C(1).(FN) = repmat(D.(FN),0,0); end
>
> C(k) = catstruct(C(k), D);
>
> The repmat of size 0 0 is to get the right class in place; if you know that you
> are going to be using catstruct immediately afterwards, you should probably be
> able to use [] (the empty array) instead of the repmat() call.

Thanks, this is probably what I will be forced to do. But this code snippet have to
be at many places be in the calling code rather than in a function, or in catstruct itself.

Sigh.

Lars

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
concatenate str... Lars Barring 24 Nov, 2008 18:20:19
rssFeed for this Thread

Contact us at files@mathworks.com