Find element in heterogeneous object array

18 ビュー (過去 30 日間)
Jérémy
Jérémy 2023 年 3 月 31 日
編集済み: Andreas Justin 2024 年 9 月 9 日
Hi,
I have a ParentClass subclassing matlab.mixin.Heterogeneous and two SubClasse1 and SubClasse2 subclassing my ParentClass.
I build a heterogeneous array of SubClasse1 and SubClasse2 objects.
How can I find in the array, elements equal to a given object being either SubClasse1 or SubClasse2.
child1 = SubClasse1;
child2 = SubClasse2;
child1 == [child1 child2]
I obtain the following error because of the heterogeneous array :
Unable to call method 'eq' because one or more inputs of class 'ParentClass' are heterogeneous and 'eq' is not sealed. For more details please see the method dispatching rules for heterogeneous arrays.
I tried to look at the method dispatching rules but how to do that for 'eq' ?
Thanks in advance for your help.
  1 件のコメント
Andreas Justin
Andreas Justin 2024 年 9 月 9 日
編集済み: Andreas Justin 2024 年 9 月 9 日
I've solved this implementing the following code in the ParentClass
methods(Sealed, Access=public)
function tf = ne(varargin)
tf = ne@handle(varargin{:});
end
function tf = eq(varargin)
tf = eq@handle(varargin{:});
end
end

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

採用された回答

Steven Lord
Steven Lord 2023 年 3 月 31 日
Something like this should work.
child1 = SubClasse1;
child2 = SubClasse2;
vectorOfObjects = [child1 child2]
isClass1 = arrayfun(@(x) isa(x, 'SubClasse1'), vectorOfObjects)
allTheSubClasse1Objects = vectorOfObjects(isClass1)
allTheSubClasse2Objects = vectorOfObjects(~isClass1)
  2 件のコメント
Jérémy
Jérémy 2023 年 4 月 3 日
Hi, thanks for your answer.
I used arrayfun too but like this:
arrayfun(@(x) child1 == x, vectorOfObjects)
Because I'm looking for a given object in the heterogeneous array and not just if it's of a matching class. But anyway it's working.
I was just wondering if there is a way to do it without arrayfun.
Steven Lord
Steven Lord 2023 年 4 月 3 日
I'd try using ismember.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by