User Defined Handle Class - Matlab OOP 12 | applied electronics engineering

Latest

User Defined Handle Class - Matlab OOP 12

By Applied Electronics - Saturday, February 11, 2017 No Comments
In this matlab object oriented programming tutorial we show how to create user defined handle class. How to view the inherited properties and methods. Also we show how the user defined handle class differs from value class.

This is part 12 tutorial of Matlab Object Oriented Programming.

Consider the user defined rect class of the previous tutorial Inheritance in Matlab Object Oriented Programming. To make this user defined class a handle class, we use the < operator to specify that it is a subclass of class handle. This is shown below.

classdef rect < handle

    properties
        l = 0;
        w = 0;
    end
    
    methods
        function rectObj = rect(v1, v2)
            if nargin == 2
                rectObj.l = v1;
                rectObj.w = v2;
            end
        end
    end
    
end

Let's create an object h1 of this class rect.


>> h1 = rect(4,5)

h1 = 

  rect with properties:

    l: 4
    w: 5


Now let's check the properties and methods:
>> properties(h1)

Properties for class rect:

    l
    w


>> methods(h1)

Methods for class rect:

rect  

Methods of rect inherited from handle.

As you can while investigating the methods of the class object h1 Matlab software shows that there are other methods associated with h1 which are inherited from inbuilt class handle.

By clicking on the underlined word Methods we can see further methods that the new object h1 can use.

Methods for class handle:

addlistener  findobj      gt           lt           
delete       findprop     isvalid      ne           
eq           ge           le           notify   


As in case of inbuilt handle classes, the user defined handle objects are reference to the same object. If a copy of the object h2 is made from h1, then both will have same properties and methods if either one's properties or method is changed.

For example, h1 object is created from user defined handle class rect. The another object h2 is created by copying h1, that is h2 = h1. When the property length l of h1 is changed then property l of h2 is also changed to same value. This is one distinguishing feature of value classes and handle classes in Matlab. We have explained Value classes Vs Handle classes in the previous tutorial. The code below illustrates this point.

>> h1 = rect(1,2)

h1 = 

  rect with properties:

    l: 1
    w: 2

>> h2 = rect(4,6)

h2 = 

  rect with properties:

    l: 4
    w: 6

>> h1 == h2

ans =

     0

>> h2 = h1

h2 = 

  rect with properties:

    l: 1
    w: 2

>> h1 == h2

ans =

     1

>> h1.l = 9;
>> h2

h2 = 

  rect with properties:

    l: 9
    w: 2

So in this way you can create user defined handle class using the < operator. You can check the properties and methods of a class object using the properties() and methods() function. In this tutorial we showed you how to view the methods of an object and proved that it inherited methods from class handle. We also showed by example how the user defined handle class was different from user defined value class.

If you liked this tutorial share it.

No Comment to " User Defined Handle Class - Matlab OOP 12 "