Properties and Methods of Object Oriented Programming Class - Matlab OOP 6 | applied electronics engineering

Latest

Properties and Methods of Object Oriented Programming Class - Matlab OOP 6

By Applied Electronics - Wednesday, February 8, 2017 No Comments
Here we extend the OOPS tutorial explaining about the Properties and Methods of Object Oriented Programming Class in Matlab.

Matlab Object Oriented Programming Class is foundation of object oriented programming with Matlab. It lets you define your own data type and operation that you can perform on those user defined class. It is essential to learn object oriented programming if you want to build smarter and larger and complex program for your work. It could be applied for any type of environment where program or software is requires such as embedded systems, typical windows program, serial interfacing, instrumentation etc.

The basics of user defined object oriented programming class was given in the previous tutorial with examples, see User defined class and object in Matlab. Also there it was mentioned that the syntax of user defined class has the following form.


classdef classname
        properties
               property1
               property2
        end

        methods
               function obj = classname(value1, value2)
                    if vargin == 2
                             obj.value1 = value1;
                             obj.propert2 = value2;
                    end
        end
end

Properties and Methods of Object Oriented Programming Class in Matlab


In the template above, there are two properties called property1 and property2. And there is only one method called classname. This method is actually a constructor- a method with same name as the class name and with default properties. A constructor is used to initialize an object of the class so that the program or compiler knows ahead what is being expected while creating an object or instances of the class.
 
As an example of the above user defined class, consider the following class.

 
classdef player
    
    properties
        name;
        age;
    end
    
    methods
        function pObj = player(var1, var2)
            if nargin == 2
                pObj.name = var1;
                pObj.age = var2;
            end
        end
    end
    
end

Viewing Properties and Methods

If you want to know the properties and functions of a class then use the properties and methods functions as follows.


>> properties(player)

Properties for class player:

    name
    age

>> methods(player)

Methods for class player:

player 

Thus the two functions properties and methods gives you the properties and methods declared in the class. These two are useful for example when you know a class but don't know what properties and methods are supported.

No Comment to " Properties and Methods of Object Oriented Programming Class - Matlab OOP 6 "