How to get unique members of an array of handle classes?

4 ビュー (過去 30 日間)
Richard Crozier
Richard Crozier 2018 年 5 月 8 日
コメント済み: Richard Crozier 2018 年 5 月 8 日
I have an array of classdef object derived from handle. It is possible that some of these members could actually point to the same object instance. I would like to get the unique members of this array. I tried the unique function, but it only works for type double.
I realise one solution is to add a method to my class, double(obj), which would allow me to use the unique function. The problem then is getting a value to return from this method. Is there some unique object identifier I can access to do this?
I'm also open to other ways to achieve the same result (a unique array of handle objects).

採用された回答

Rik
Rik 2018 年 5 月 8 日
Since 2013a the unique function supports objects. Before that, you could try a nested loop with calls to isequal, or indeed convert to double (which will only work on pre-2014b).
close all
f=figure;
f2=figure;
h=f;
h2=f;
unique_handle_array=unique([f,f2,h,h2])
%returns handles to figure(1) and figure(2)
  1 件のコメント
Richard Crozier
Richard Crozier 2018 年 5 月 8 日
You're right, actually I was being fooled by another error I had made in my code.

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

その他の回答 (0 件)

カテゴリ

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