How many instance of the class is initiated?

5 views (last 30 days)
Hello
I am just writing an object oriented code in Matlab. I need every object of my classes to know how many instances of that particular class has been made. I have tried persistent data variables or static methods. But no use! I appreciate any help.
Thank You
Hadi
  5 Comments
Hadi Hajieghrary
Hadi Hajieghrary on 30 May 2013
It came up when I was writing a code on my research in. I did the same in VB.Net before, and I was wondering if it is possible here in Matlab too. Imagine you have a simulation scene in which the objects know how many of them exists. I have a good use for this. I am working in multi-agent systems, and this could be very useful for me.
Sean de Wolski
Sean de Wolski on 30 May 2013
Then why not have these ovbjects be children of a parent object like in handle graphics?

Sign in to comment.

Answers (2)

Sean de Wolski
Sean de Wolski on 30 May 2013
Here is how I would attack this, (and let me reiterate, I don't know why you need this, there is probably a better way).
I would have singleton class:
That stores members of the other class. Let's call the singleton class, S.
Now you create one of your objects that needs to know about others, let's call it K. K first finds if an object of class S exists, if it does not, then it creates one. If it does, it calls a static method of this S object that bumps up the count. It also, adds a listener to S listening for it's own being deleted event. Now if this object of class K is deleted, the S object can remain up to date.
Alternatively, you could use findobj as is described here to look for some dummy property that all K objects have:
This is probably the easier way to do this.
  2 Comments
Hadi Hajieghrary
Hadi Hajieghrary on 30 May 2013
I have seen this code of you. I appreciate. Bet the problem, as I mentioned above is when you are deleting on of the instance. When there is a persistent variable in the memory, Matlab is not going to release the thread to that object and consequently you are not able to delete the object. Even if you use the "Static Method" and "Persistent" Variable. It seems even though the static method is a general for the class, the matlab still recognize it as the objects', and then an object couldn't delete itself.
"findobj" is a reasonable way.
Sean de Wolski
Sean de Wolski on 30 May 2013
No, the static method of the singleton object.

Sign in to comment.


Hadi Hajieghrary
Hadi Hajieghrary on 30 May 2013
Hi
I have gone over the Matlab documents and so far I have understand these:
For this task we need a variable with global scope. This is not a good idea, because I want just my class to access it. What I used was a "Static" method along with a "Persistent" variable in it. In this way, in any instant you could just call "MyClass.Method" and find out the value of it. But, and this is a Big BUT... In my case which I wanted to know how many instant of the class have been made so far, this works up until you want to clear an object of the class. Then you should decrease the variable by calling this static method. And when you have called this method you could not delete the object!!!! And the big BUT is this contradiction. I gave up on this because I don't have more time to spend on it. And, I "strongly recommend"!! :-) you to do so, or use the "Sluggish" Global variable if you need it so bad.
Hadi

Products

Community Treasure Hunt

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

Start Hunting!