5

Possible Duplicate:
How to modify properties of a Matlab Object

I'm trying to convert my C# code into Matlab, in Matlab I decided to use OOP, which I haven't been used with Matlab, to be able to handle with the complexity of my C# code.

Looking the tutorial, I come up with the following code:

classdef Cat
    properties
        meowCount = 0; 
    end
    methods 
        function Meow(C)
            disp('meowww'); 
            C.meowCount = C.meowCount + 1;
        end
    end    
end

The result:

>> c = Cat();
>> c.Meow();
meowww
>> c

c = 

  Cat

  Properties:
     meowCount: 0

  Methods

So, meowCount does not change. What is the problem?

Community
  • 1
  • 1
Sait
  • 19,045
  • 18
  • 72
  • 99
  • 2
    You have to inherit from `handle` if you want to be able to update `Cat.meowCount` as you are trying to do in your example code. – Chris Aug 23 '12 at 12:02
  • 1
    @Chris, yes. Both of your comments are true. It is duplicate of the same question, sorry. And it worked after using `handle`. Thanks. – Sait Aug 23 '12 at 12:05

1 Answers1

9

A few problems I noticed:

  • you have no constructor
  • you do not derive from the handle base class

The constructor is not strictly necessary, but very useful to get to know for when you really want to start developing larger classes. It is used to initialize the obj object, which gets passed around to each and every method. It is quite similar to Python's self, or C++'s this.

So, your corrected class:

classdef Cat < handle

    properties
        meowCount = 0; 
    end

    methods 

        function obj = Cat()
            % all initializations, calls to base class, etc. here,
        end

        function Meow(obj)
            disp('meowww'); 
            obj.meowCount = obj.meowCount + 1;
        end
    end    
end

Demonstration:

>> C = Cat;
>> C.Meow; 
meowww
>> C.meowCount
1
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • 2
    There is no need for a constructor and the OP can use `C` or `obj`, it is just a naming convention. The lack of inheritance is the reason for the problem, as I mention in my comment to the OP. Because this is a duplicate question we should vote to close rather than answer. – Chris Aug 23 '12 at 12:12
  • @Chris, is that a better idea for me to delete my question instead of voting for an answer? Thanks. – Sait Aug 23 '12 at 12:14
  • 1
    @ Chris when did naming conventions become *"just"* naming conventions? It is **very** fruitful if everyone uses the same name to mean the same thing (hence the term "convention"). Also, a constructor, although not *necessary*, is still something that'll sure be useful later on. But you're right of course -- it's a dupe, and should be removed. – Rody Oldenhuis Aug 23 '12 at 12:18
  • 1
    In the future it will probably be best to do this if you notice you have posted a duplicated. However, you won't be able to now your question has an upvoted answer (I think). If you wait a bit this question may get enough close votes so you don't have to do anything. Have a read through [this meta post](http://meta.stackexchange.com/q/230/181221) for a discussion on deleting duplicates. – Chris Aug 23 '12 at 12:22
  • @RodyOldenhuis Sorry if I was a bit terse. Of course it is best if everyone follows the same conventions. I just felt that since the solution to the OP's question is to inherit from `handle`, this should have been the main point of your answer. The other points should be mentioned as conventions and recommended. However, at the moment your answer seems to suggest that these will go some way to solve the problem the OP is having, which they don't. – Chris Aug 23 '12 at 12:25
  • @Chris this sort of thing can be discussed *at length* of course, which we shouldn't do here. Nevertheless, in my defense, I always make an attempt to adjust my answers to the probable level of understanding of the OP. Adding a few extra rules here and there, while not strictly necessary to solve the problem, will sure increase the level of understanding of the OP in the long run. – Rody Oldenhuis Aug 23 '12 at 12:37
  • Note that the answer still wrongly claims that a constructor is necessary. It isn't. Also, instead of creating a handle class, the OP could simply have the methods return an updated copy of the object. – Jonas Aug 23 '12 at 14:37