Creating and Modifying Handles to an object in Matlab - Matlab OOP 2 | applied electronics engineering

Latest

Creating and Modifying Handles to an object in Matlab - Matlab OOP 2

By Applied Electronics - Monday, February 6, 2017 No Comments
In the following we explain Creating and Modifying Handles to an object in Matlab. For illustration purpose we use the Matlab inbuilt figure object.

See the first tutorial- Object oriented programming with Matlab - Introduction

In Matlab, figure is an object. You use this object to create graphs and plots of your variables.

We can create a handle to the figure object by giving a variable name to the object's handle and assign it to the object figure. What it does is that, Matlab stores the figure object in the handle variable. The following examples illustrate this.

First create a handle to the figure object as follows,

>> f = figure;

Here, f is a handle variable of the figure objects. It stores the figure objects. Handle is a variable object container or a pointer to the object.

The handle f belongs to class called matlab.ui.Figure. To know the class in which it belongs to type the following,

>> class(f)

ans =

matlab.ui.Figure

To see the handle f properties type the following,

>> properties(f)

Properties for class matlab.ui.Figure:

    Position
    Units
    Renderer
    RendererMode
    Visible
    Color
    CloseRequestFcn
    CurrentAxes
    CurrentCharacter

To see its methods type the following,

>> methods(f)

Methods for class matlab.ui.Figure:

Figure       display      java         
addlistener  double       ne           
addprop      eq           reset        
cat          findobj      set          
clo          get          vertcat      
details      horzcat      
disp         isprop       

Static methods:

loadobj      

Methods of matlab.ui.Figure inherited from handle.

How to Create and Modify Handles to an object in Matlab?


Above we explained handle a little bit and how to know its class, properties and method. Once you properties and methods that a handle to an object has you can modify its properties or create new ones.

There are two ways in which you can create or modify handle properties and methods. The first is using get() and set() functions. The other is using dot convention.

The get() function can be used to access information about the handle. For example if you want to get the Color attributes information of figure object handle f in the previous example then you would type the following.

>> get(f, 'Color')

ans =

    0.9400    0.9400    0.9400

To set the Color property of the handle f to different value you will use the set() function as follows,

>> set(f, 'Color', [0.3 0.5 0.9])
>> f

f = 

  Figure (1) with properties:

      Number: 1
        Name: ''
       Color: [0.3000 0.5000 0.9000]
    Position: [403 246 560 420]
       Units: 'pixels'

  Show all properties

These two examples showed you how to access property information of the handle and how you can modify them.

The other methods that you can use to achieve the same thing is the dot. This is same convention as used in other object oriented programming like c++ or C#. Here we illustrate how to use these in Matlab software.

To access information about property of the handle such as color like in previous example you should type the following.

>> f.Color

ans =

    0.9400    0.9400    0.9400

Now to change this value you would type the following.

>> f.Color = [0.9 0.4 0.2]

f = 

  Figure (1) with properties:

      Number: 1
        Name: ''
       Color: [0.9000 0.4000 0.2000]
    Position: [403 246 560 420]
       Units: 'pixels'

  Show all properties

So in this way you can create a handle to object, access their information, create and modify handles to an object in Matlab Software.

For understanding why it is necessary to create handles in Matlab see What are handles in Matlab and why they are used?


No Comment to " Creating and Modifying Handles to an object in Matlab - Matlab OOP 2 "