Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: How to merge two structures?
Replies: 4   Last Post: Jan 4, 2013 5:58 PM

 Search Thread: Advanced Search

 Messages: [ Previous | Next ]
 Jos Posts: 627 Registered: 7/28/07
Re: How to merge two structures?
Posted: May 23, 2008 5:36 AM
 Plain Text Reply

"Bjorn Gustavsson" <bjonr@irf.se> wrote in message
<g160fs\$956\$1@fred.mathworks.com>...
> "Dimitri Shvorob" <not.dimitri.shvorob@gmail.com> wrote in
> message <g15vpb\$63q\$1@fred.mathworks.com>...

> > .. i.e. if I have structure x
> > x.a
> > x.b
> >
> > and structure y
> > y.c
> > y.d
> >
> > how can I get structure z
> > z.a
> > z.b
> > z.c
> > z.d
> >
> > ? If there's no easy way to do that, maybe someone could
> > code it up and post on FEX? If the code could not handle
> > arrays of structures or require no common fields in x and
> > y, that would be fine.
> >

> Dimitri, there is already such a utility:
>
>

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=7842&objectType=file
>
> If it handles arrays of structs, but I've used it without
> problems for my pedestrian use of merging two structs.
>
> HTH,
> Bjeorn

Thanks Bjeorn, for pointing out my CATSTRUCT function.
Dmitri, I hope you find this function useful.

At present CATSTRUCT cannot handle arrays of structures. So,

x(1).a = 1 ; x(2).a = 2 ;
y(1).b = 10 ; y(2).b = 20 ;
z = catstruct(x,y)

errors, although I can imaging that it might be useful to
return the correct z. However, you can use a simple-for loop
for this:

for i=1:2,
z(i) = catstruct(x(i),y(i)) ;
end

provided that the structure arrays x and y have the same
number of elements.

hth
Jos

http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectType=author&objectId=1094450

Date Subject Author
5/23/08 Dimitri Shvorob
5/23/08 Bjorn Gustavsson
5/23/08 Jos
5/23/08 Dimitri Shvorob
1/4/13 james bejon

© The Math Forum at NCTM 1994-2018. All Rights Reserved.