How can I find all object of a certain handle class?

31 Ansichten (letzte 30 Tage)
royk am 5 Aug. 2019
Kommentiert: royk am 7 Aug. 2019
I need to find all current object memory instances of a certain handle class "MyClass".
I cannot use "findobj" because I do not have a "seed" handle.
using "whos" is also problematic because: (a) different variables can in fact point to the exact same object; (b) objects may exist without any variable poitnitng to them (for example objects that are pointed to from a "UserData" of a figure); (c) handles to soem objects may be held in persitence variables of a static method of the class.
is there a generic way to find all the objects of MyClass despites these listed difficulties?
  7 Kommentare

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume am 5 Aug. 2019
I don't think there's any way to track down instances after the fact.
You could however track the creation and deletion of all instances of any handle class, without changing the class code, with meta.class and the events InstanceCreated and InstanceDestroyed. The whole thing is badly documented but the 2nd argument of the event is of type ClassInstance whose one of the property is the object created/destroyed.
Unfortunately, for the Destroyed case, the handle is no longer valid by the time you get it (since the object has been destroyed), so overall, I'm not sure it's that useful. Still, you could keep a reference count of object creation and destruction to know how many are in memory.
  7 Kommentare
royk am 7 Aug. 2019
Very nice implementation!
probem solved. much thanks
one suggestion: the handle of MyClass to the controller can be a Constant property of MyClass. this makes it all more elegant avoiding the need for the getController function and the persistence variable

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)




Community Treasure Hunt

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

Start Hunting!

Translated by