How to concatenate multidimensional struct arrays

7 views (last 30 days)
Hi All, I have:
A = 1 * x array with k properties (i.e., fieldnames)
B = 1 * y array with (correspondingly same) k properties
How can I concatenate A and B to give C, such that C = 1 * (x+y) array with (correspondingly same) k properties.
Thank you in advance.
  4 Comments
Guillaume
Guillaume on 15 May 2017
Well, this is even more confusing. From the error message you mention and from your comment above, it would appear that A and B are instances of classes which is yet another data type completely different from structures or cell arrays.
If that is the case, we need to see the definition of the classes (at the very least the property bit and the class hierarchy).
Sherif
Sherif on 15 May 2017
Edited: Sherif on 15 May 2017
Thanks Guillaume. I'm actually trying out something, and perhaps I am mixing things up. Both A and B are handle classes. Each has 35 properties, and in a 21-count loop, giving 1*21 arrays each. What i want to do is to add the corresponding properties in A and B, such that I now have 1*42 arrays with the same 35 properties.
My reading suggests MATLAB does not support C=[A,B] in this case, except if A and B are subclasses in a heterogeneous class array/hierarchy like in this help page
At the moment, no definitions such as (Abstract, SetAccess=private) or whatsoever for the properties, but they can be referenced as A.property1, A.property2, B.property1, B.property2 etc.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 15 May 2017
Right, so you do have instances of classes, A of class segment and B of class segment2 (poor naming) in package elements. Several questions:
  • Any reason for the two different classes. Can't A and B be instances of the same class. That way all your problem about concatenation go away.
  • If A and B must be instances of different classes, then what class should the concatenation be? Instance of segment, instance of segment2 or instance of some other class? If there's no reason for selecting one over the other, then there is something wrong with your class design and we're better off working on fixing that design than working on getting the concatenation to work.
This help page details the rules for concatenation of objects. In theory, for the concatenation to work, all you need to do is provide a converter in each class (but see my point above).
You can indeed derive both classes from a base class derived from matlab.mixin.Heterogeneous, which would also enable the concatenation but you still have to define a hierarchy between all these classes. Considering the poor naming of the classes, that hierarchy is not obvious at the moment.
  2 Comments
KL
KL on 15 May 2017
Thanks for the explanation Guillaume
Sherif
Sherif on 18 May 2017
Edited: Sherif on 18 May 2017
Thanks Guillaume. The guide was helpful.
B of class segment2 was intended in order to flexibly change several of the properties of A of class segment, such that A and B have same properties but different values. At the moment, it appears using just A and changing needed properties' values using for loop is sufficient. In this case, A and B are instances of the same class segment, and no more need to concatenate. Code works fine.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 11 May 2017
  3 Comments
Sherif
Sherif on 15 May 2017
Name Size Bytes Class Attributes
A 1x21 272 elements.segment
B 1x21 272 elements.segment2

Sign in to comment.

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!