How do I track the number of instances of a given handle?
12 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I'm wondering if there is a way to track the number of instances of a handle. Example:
% Create an instance of the handle here:
handleObj = handleClass;
% Add a reference to it here:
handleObj2 = handleObj;
I want some kind of property stored in the handle that would now tell me there are 2 instances of it in existence, and each time I delete one it will decrement. Is this possible?
2 Commenti
Walter Roberson
il 17 Ott 2019
See the discussion in https://www.mathworks.com/matlabcentral/answers/474974-how-can-i-find-all-object-of-a-certain-handle-class
See also https://www.mathworks.com/matlabcentral/answers/383188-mex-inplace-change-problems-r2015b-and-later for information on the perils of references.
Risposte (1)
Guillaume
il 17 Ott 2019
I'm curious what you mean by "inheriting that class appears to put a lot of constraints on what I can do with my existing code in terms of overloads."?
However, note that your question doesn't match your example code. With this code:
% Create an instance of the handle here:
handleObj = handleClass;
% Add a reference to it here:
handleObj2 = handleObj;
there is still only one instance of the handle class. The two variables share the same instance.
If that is what you want to do, count the number of variables pointing to the same instance, then I'm afraid there's no way to do that. There's no mechanism at all in matlab to get information when the ref. count of variable gets incremented/decremented.
On the other hand, if what you want to do is indeed count the number of instances:
handleObj = handleClass; %one instance created
handleObj2 = handleObk; %still only one instance
handleObj3 = handleClass; %New instance, so now 2 instances
delete(handlObj); %Now just one instance, both handleObj and handleObj2 are now invalid handles
then the InstanceCreated and instanceDestroyed events of the meta class would do the job.
0 Commenti
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!