## Is there any clean and simple way to catenate two conformable structures or structure arrays?

### Is there any clean and simple way to catenate two conformable structures or structure arrays?

I'd like to take two structures and make a new structure whose fields are
the (union of) the fields of the two original structures. I've tried playing
with dynamic field names using the .(expression) notation and the fieldnames
function, but the expression must resolve into a single field name, not a
comma-separated list. I get this error...

Quote:>> a.b=1

a =
b: 1
Quote:>> a.c=2

a =
b: 1
c: 2
Quote:>> a.(fieldnames(a)) = pi

??? Argument to dynamic structure reference must evaluate to a valid field
name.

I see how it could be done with struct, fieldnames, and struct2cell, but
that's pretty messy. Does anyone know/see a better way?

Mathworks, is this a capability you could include in the future?

Thanks,

Ken

### Is there any clean and simple way to catenate two conformable structures or structure arrays?

Quote:> I'd like to take two structures and make a
> new structure whose fields are the (union
> of) the fields of the two original
> structures.

I feel silly for even suggesting this:

names=fieldnames(b);
for i=1:length(names)
eval(['a.' names{i} '=b.' names{i}]);
end;

### Is there any clean and simple way to catenate two conformable structures or structure arrays?

Quote:> > I'd like to take two structures and make a
> > new structure whose fields are the (union
> > of) the fields of the two original
> > structures.

> I feel silly for even suggesting this:

> names=fieldnames(b);
> for i=1:length(names)
>    eval(['a.' names{i} '=b.' names{i}]);
> end;

Thanks, Michael. Looping may indeed be the best option and I may go ahead
and write a function with a loop, but I thought I'd first check here for
something without loops that uses some subtle, unknown (to me) feature of
the language.

Cheers,

Ken

### Is there any clean and simple way to catenate two conformable structures or structure arrays?

> names=fieldnames(b);
> for i=1:length(names)
> eval(['a.' names{i} '=b.' names{i}]);
> end;

... it may be beyond the reach of my cerebral cortex - but: doesn't

a=b;

do the same?
us

### Is there any clean and simple way to catenate two conformable structures or structure arrays?

>> I'd like to take two structures and make a
>> new structure whose fields are the (union
>> of) the fields of the two original
>> structures.

> I feel silly for even suggesting this:

Shame on you for using EVAL :)

Quote:> names=fieldnames(b);
> for i=1:length(names)
> eval(['a.' names{i} '=b.' names{i}]);
> end;

Here's my first approximation. It uses a FOR loop, but it shouldn't
be bad unless one or both of the structures are large ... and it
shouldn't be that bad unless both structures are large. If only one
is large, use it as the A structure below.

A=struct('field1',1,'field3',2);
B=struct('field2',1,'field4',2);
clear C

C = A;

F=fieldnames(B);
for i=1:length(F)
C.(F{i}) = B.(F{i});
end

Use ISFIELD to detect a field in B that's already in C and handle it
however you want.

--
Steve Lord

### Is there any clean and simple way to catenate two conformable structures or structure arrays?

maybe:

% two STRUCTs
a.b=1;
a.c=2;
b.x=10;
b.y=20;
b.z=30;
% concatenate
fn=[fieldnames(a);fieldnames(b)];
% fn=unique(fn);
fnl=length(fn);
ns=cell2struct(cell(fnl,1),fn)
% n =
% b: []
% c: []
% x: []
% y: []
% z: []

us

> I'd like to take two structures and make a new structure whose

fields are the (union of) the fields of the two original structures.

### Is there any clean and simple way to catenate two conformable structures or structure arrays?

> > names=fieldnames(b);
> > for i=1:length(names)
> > eval(['a.' names{i} '=b.' names{i}]);
> > end;

> ... it may be beyond the reach of my cerebral cortex - but: doesn't

>      a=b;

> do the same?
> us

a = b will replace a with b, thus losing the fields that belonged to a. -
Ken

### Is there any clean and simple way to catenate two conformable structures or structure arrays?

> a = b will replace a with b, thus losing the fields that belonged
> to a. -
> Ken

sorry, must have misinterpreted your example, which states (in
summary):

Quote:>> a.b=1;
>> a.c=2;
>> a.(fieldnames(a)) = pi;

without a <b> anywhere ...
now - i guess - it should have read
b.(fieldnames...)
us

### Is there any clean and simple way to catenate two conformable structures or structure arrays?

Quote:> I'd like to take two structures and make a new structure whose fields are
> the (union of) the fields of the two original structures. I've tried
playing
> with dynamic field names using the .(expression) notation and the
fieldnames
> function, but the expression must resolve into a single field name, not a
> comma-separated list. I get this error...

> >> a.b=1
> a =
>     b: 1
> >> a.c=2
> a =
>     b: 1
>     c: 2
> >> a.(fieldnames(a)) = pi
> ??? Argument to dynamic structure reference must evaluate to a valid field
> name.

> I see how it could be done with struct, fieldnames, and struct2cell, but
> that's pretty messy. Does anyone know/see a better way?

> Mathworks, is this a capability you could include in the future?

> Thanks,

> Ken

Here's the best I've come up with, but it's still not "pretty". Perhaps a
clean cat of structs can be part of a future Matlab version.

Quote:>> a = struct('b', 1, 'c', 2)

a =
b: 1
c: 2
Quote:>> d = struct('e', 3, 'f', 4)

d =
e: 3
f: 4
Quote:>> a = cell2struct([struct2cell(a); struct2cell(d)], [fieldnames(a);

fieldnames(d)], 1)
a =
b: 1
c: 2
e: 3
f: 4
Ken

### Is there any clean and simple way to catenate two conformable structures or structure arrays?

> Here's the best I've come up with, but it's still not "pretty".
...
>>> a = cell2struct([struct2cell(a); struct2cell(d)],
> [fieldnames(a);
> fieldnames(d)], 1)

... i thought my solution looked a bit less messy ...
:-)
us

I have got two structures:
POLY.X = DATA.X
POLY.Y = DATA.Y
POLY.Z = ????

which numbers are real, and knowing
that:POLY.X = [1.25.. 54.25.. ...]
POLY.Y and POLY.Z the sames ??

regards