How to concatenate multidimensional struct arrays

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 comentarios

Matt J
Matt J el 11 de Mayo de 2017
The title to your post talks about cell arrays, but your question appears to talk about struct arrays.
Sherif
Sherif el 15 de Mayo de 2017
Thanks Matt. Yes, they are struct arrays. From my readings, it appears I have to use heterogeneous class hierarchies, with A and B are subclasses; that only then would MATLAB support concatenating them (i.e., A and B).
Guillaume
Guillaume el 15 de Mayo de 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 el 15 de Mayo de 2017
Editada: Sherif el 15 de Mayo de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 15 de Mayo de 2017

2 votos

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 comentarios

KL
KL el 15 de Mayo de 2017
Thanks for the explanation Guillaume
Sherif
Sherif el 18 de Mayo de 2017
Editada: Sherif el 18 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 11 de Mayo de 2017
C=[A,B]

3 comentarios

Sherif
Sherif el 11 de Mayo de 2017
Thanks Matt,
I did not include that the field names have different properties.
For example:
A.id & B.id may be a vector, with id{1,2,...,x} array
A.age & B.age may be strings, with age{1,2,...,x} array
A.height & B.height may be numbers, with height{1,2,...,x} array
Concatenating [A, B] does not seem to solve it.
The resulting error has been:
Error using horzcat
The following error occurred converting from B to A:
Too many input arguments.
Perhaps because A and B have many field names K=35, while x=21, y=42.
Sherif,
Please show me the output of
>>whos A B
Name Size Bytes Class Attributes
A 1x21 272 elements.segment
B 1x21 272 elements.segment2

Iniciar sesión para comentar.

Categorías

Más información sobre Construct and Work with Object Arrays en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 11 de Mayo de 2017

Editada:

el 18 de Mayo de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by