Demo: How to create objects using OOP in MATLAB in under 10 minutes!

Introduction

In this post, a demonstration on how to create objects using object oriented programming (OOP) in MATLAB is covered using easy to follow steps, and the basic MATLAB syntax. The demo is structured as follows:

  • creating objects using the basic MATLAB syntax,
  • hierarchy in objects (including Abstract method functions), and
  • initialising and validating property values.

OOP is an approach to programming, where focus is put on design patterns and classification systems. It forms a consistent framework which is recognisable, and can be easily reused and extended by others. OOP is ideal to use when designing complex models, because of the flexibility and extensibility of the code, where a simple model can easily be extended to cover a more complex model.

Data

In object oriented programming the classification of data and the actions to be performed on the data, is done by defining different objects. An object is defined as an instance of a class which has specific properties and functions associated with it.

Defining classes

A class is used to define an object in terms of:

  1. the data it contains (properties),
  2. the operations performed on the data (methods/functions), and
  3. events and listeners (not covered in this demo).

Classes, properties, methods, and events and listeners all have optional attributes which control the behaviour of the specific methods.

Figure: Objects in MATLAB

Figure: Class, Properties and Methods optional attributes

Classdef in MATLAB

In MATLAB, a class is defined using the classdef method under which the name of the class, the properties of the object in the class, and the functions to be applied to the objects is defined.

Example: Defining a Rectangle Class

Consider a rectangle as an object as defined by the class Rectangle. To define this object, one must firstly determine the data contained by the object and secondly, the actions to be performed on the data of that object.

  1. The data values (properties) for a rectangle object:
    • the height of the rectangle,
    • the width of the rectangle, and
    • the colour of the rectangle.
  2. We are interested in performing the following action (function) on the data contained by the rectangle object:
    • calculating the area of the rectangle.

Class properties

Objects which share common properties are defined under a specific class, the common properties and the attributes of those properties are defined under the class properties block of code.

Example: Defining a Rectangle Class

The following properties are used to describe the rectangle namely width, height and colour.

Properties definition code in MATLAB: Rectangle

classdef Rectangle
    % Rectangle Data set class
    properties
        Width       %width of rectangle
        Height      %height of rectangle
        Colour      %colour of rectangle
    end
end
  • The property command can be used to see a list of the defined properties for a class, for example the properties associated with the class Rectangle can be obtained as follows:

Command in MATLAB:

properties(Rectangle)

Output in MATLAB:

Properties for class Rectangle:

Colour

Width

Height

Class methods

Functions to be applied to the data contained within the objects of a specific class are defined under the class methods block of code.

In the example of a rectangle, we might be interested in calculating the area of the rectangle. The function getArea is written in the methods block as follows:

Methods and functions code in MATLAB: getArea

 methods
        function Area=getArea(thisRectangle)
            Area=thisRectangle.Width*thisRectangle.Height; 
        end 
    end
  • Method functions can be called in the command window using the name of the method function to be called, for example the function getArea for the rectangle object thisRectangle can be called as follows:

Command in MATLAB:

Area=getArea(thisRectangle);
  • Alternatively, the area for the rectangle object thisRectangle using the function getArea can be computed as follows:

Command in MATLAB:

thisRectangle.getArea;
  • The methods command can be used to see a list of the defined method functions for a class, for example the method functions associated with the class Rectangle can be obtained as follows:

Command in MATLAB:

methods(Rectangle)

Output in MATLAB:

Methods for class Rectangle:

Rectangle getArea

The functions listed under methods include Rectangle and getArea. Rectangle is a default built-in constructor function used to create objects, in the Section: Creation of Objects, the customisation of the constructor function is discussed.

In conclusion, the MATLAB code for the class definition of Rectangle will look as follows:

Class definition code in MATLAB: Rectangle

Creation of Objects

Objects can be created/instantiated by specifying an object to be a member of a specific class, or alternatively objects can be created using the constructor method function.

  1. Objects can be created in the command window/script file in MATLAB, and the dot syntax can be used to assign and access properties of the objects.

Example: Rectangle Object

Creating an instance for a specific rectangle object, named Rectangle1:

Command in MATLAB:

Rectangle1=Rectangle

Output in MATLAB:

Rectangle1 =

Rectangle with properties:

Width: []

Height: []

Colour: []

An object called Rectangle1 will be created with properties Width, Height and Colour containing empty values.

The data values associated with the Rectangle1 object can be assigned and accessed using the dot syntax in MATLAB. The Width property of the Rectangle1 is assigned the value 5, the Height property of the Rectangle1 is assigned the value 7, and the Colour property is assigned to be green.

Commands in MATLAB:

Rectangle1.Width=5;

Rectangle1.Height=7;

Rectangle.Colour='green';

If we were to run the command to display Rectangle1 in MATLAB now, the following will be displayed:

Command in MATLAB:

Rectangle1

Output in MATLAB:

Rectangle1 =

Rectangle with properties:

Width: 5

Height: 7

Colour: 'green'
  1. Creating objects using the constructor method.

A constructor method function is defined under the class methods block of code, and it is used to create/instantiate new objects. In defining a constructor method function, the name of the constructor function must be assigned the same name as the corresponding class name.

Example: Rectangle Constructor function

A constructor function for the class Rectangle is defined below, where the name of the constructor function is assigned to be Rectangle:

Constructor function code in MATLAB: Rectangle

function thisRectangle=Rectangle(colour,width,height)
    if nargin ==3
        thisRectangle.Colour=colour;
        thisRectangle.Width=width;
        thisRectangle.Height=height;
    end
end

For example, if we were to create Rectangle1 using the constructor function, we would assign the values for the Width, Height and Colour properties of the rectangle by specifying them as input arguments in the constructor function. In contrast to the steps taken to create the object Rectangle1 in 1) above, using the constructor function, the creation and assignment of property values of an object can be performed in one step.

Command in MATLAB:

Rectangle1=Rectangle('green',5,7)

Output in MATLAB:

Rectangle1 =

Rectangle with properties:

Width: 5

Height: 7

Colour: 'green'

In addition to creating objects and initialising object property values, the constructor method function can also be used for validating property values. This enables one to perform error checking when an object is created to ensure the object created fills the template (class) of the object correctly. Error checking can be performed to ensure names of objects are correctly spelled, and data values are stored as the correct class type.

This is an advantage over the procedural programming approach as using the procedural programming approach, misspelling of a variable will lead to the creation of a new variable and data values assigned incorrectly will not necessarily be noticed.

To summarise, we have created an object called Rectangle1 which is an instance of a Rectangle object as defined by the class Rectangle:

Class definition code in MATLAB: Rectangle

 

This is the first of two blog on OOP in Matlab. In the next blog we consider how subclasses and super-classes can be defined as we take a closer look at the hierarchy in MATLAB. Continue reading...

References

(1) MATLAB: Object Oriented Programming (R2016b). 9th ed. Natwick, MA: MathWorks, 2016.

2 thoughts on “Demo: How to create objects using OOP in MATLAB in under 10 minutes!

Leave a Reply

Your email address will not be published. Required fields are marked *