MATLAB: How to find all object of a certain handle class

find objectshandle classMATLABoop

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?

Best Answer

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.