How to concatenate multidimensional struct arrays

7 ビュー (過去 30 日間)
Sherif
Sherif 2017 年 5 月 11 日
編集済み: Sherif 2017 年 5 月 18 日
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 件のコメント
Guillaume
Guillaume 2017 年 5 月 15 日
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 2017 年 5 月 15 日
編集済み: Sherif 2017 年 5 月 15 日
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.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2017 年 5 月 15 日
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 件のコメント
KL
KL 2017 年 5 月 15 日
Thanks for the explanation Guillaume
Sherif
Sherif 2017 年 5 月 18 日
編集済み: Sherif 2017 年 5 月 18 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2017 年 5 月 11 日
  3 件のコメント
Matt J
Matt J 2017 年 5 月 11 日
Sherif,
Please show me the output of
>>whos A B
Sherif
Sherif 2017 年 5 月 15 日
Name Size Bytes Class Attributes
A 1x21 272 elements.segment
B 1x21 272 elements.segment2

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by