User defined class and object in Matlab - Matlab OOP 5 | applied electronics engineering

Latest

User defined class and object in Matlab - Matlab OOP 5

By Applied Electronics - Tuesday, February 7, 2017 No Comments
We show here how to create user defined class and object in Matlab. Creating of class and object in Matlab is similar to other object oriented programming language. Object oriented programming is key to building larger complex programs. It is also required if you want to create your own data types.

This is the fifth tutorial on Object Oriented programming with Matlab. For the introduction to matlab classes and object see Object oriented programming with Matlab - Introduction, the 2nd and 3rd tutorial on handles- Creating and Modifying Handles to an object in Matlab and What are handles in Matlab and why they are used?

Here we will show the basic of simple class and how to make an object of that class. After creating an object of instance of the class we will show how to assign value to properties of the class. At the end we show how to make constructor for the class. So we will be illustrating the basic framework of creating class in Matlab. Creating more complicated involves public, private, protected access and write ability and others which will be covered in next blog post.

In Matlab, the keyword classdef is used to define a class. The keyword classdef is followed by the name of the class. Inside the classdef we put the properties and methods. Inside the method we can optionally define the constructor.

The syntax of defining user defined class is thus,

classdef classname
        properties
               property1
               property2
        end

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

User defined class and object in Matlab Example 1


For example a class person in matlab is then,

classdef person
    properties
        name;
        age;
    end
    methods
        function pObj = person(val1, val2)
            if nargin == 2
                pObj.name = val1;
                pObj.age = val2;
            end
        end
    end
end

In the example above, we created a class called person. The class person has two properties called name and age. These two properties are not initialized to default values. For example we could have initialized these to some values like:

name = 'jon';
age = 20;

The class person has one property which is the constructor. A constructor is a default method used to initialize the class object with default value. This constructor is also optional and we could have avoided it. The constructor is itself an object created as method using the keyword function. It has the same name of that of the class which in this case is person. The input argument of the constructor are the default values for the properties. So when we create an object of class person later on we can specify values for the properties because of the constructor. The two lines below the constructor declaration is what assigns the supplied values to the properties of the class. The if statement is merely used that the number of argument in the function person is indeed 2. Since the function has the 2 arguments the two lines code within the if statement are executed.

Creating the Object

 Once we have created the class and saved the file with same name as the class we are ready to create a new object.

Objects is created by choosing object name and assigning it the class. For example let the object name be person1 then the following creates an object person1 of class person:

>> person1 = person

person1 = 

  person with properties:

    name: []
     age: []

The person1 properties are displayed because the statement person1 = person is not terminated with the semicolon ;

Also notice that the object person1 name and age are empty. This is because we have not assigned any name or age to them. To assign the properties name and age to the object we do it in the following manner.

>> person1.name = 'jon'

person1 = 

  person with properties:

    name: 'jon'
     age: []

>> person1.age = 20

person1 = 

  person with properties:

    name: 'jon'
     age: 20

So in this way you can create a class and object of that class.

User defined class and object in Matlab Example 2


It is not always to required to declare the methods of the class. That is we can simply just specify the properties only. This then becomes the simplest class that could be written. So for example the following class player is valid.


classdef player
    
    properties
        name;
        age;
    end
end

As you can see there is no methods and there are two properties which are not initialized.

Once we have created a class we create an object or instance of the class by writing,

>> player1 = player;

Once we have done so we can use the properties defined in the class. For example we can specify the name and age for the object player1. This is done in the following way.

>> player1.name = 'jon';

and,

>> player1.age = 20;

We can check to verify that the player1 object name is indeed jon and age is indeed 20 by doing the followings:

>> player1.name

ans =

jon

and for age,

>> player1.age

ans =

20

See the following for doing this in Matlab Software:







No Comment to " User defined class and object in Matlab - Matlab OOP 5 "