Introduction to Object-Oriented Programming in MATLAB

When creating software applications, it is important to organize the various building blocks of your software into related groups. For example, a custom numerical solver may require several configuration parameters and routines to perform its full set of calculations. Object-oriented programming (OOP) allows you to group the solver’s configuration parameters (properties) with its functions (methods) into a single definition, or class. Everything a user will need to properly execute this solver is defined in this class. An object is an instance of a class. When a program executes, the object is created based on its class definition and behaves in the way defined by the class. The properties of an object represent its state, and its methods represent all the actions a user may perform. In this way, a code author can easily group all the related data and functions for a software system and a user can easily find and use all the capabilities the code author has developed. The following example uses object-oriented programming to build an application that will analyze sensor data from an array of sensors. The code used in this article is available for download.

Application Example: Analyzing Sensor Array Data

A sensor array (Figure 1) is a collection of sensors, often arranged in a line, that is used to sample a medium such as air, water, or the ground for radar, sonar, or cellular communications. By collecting time samples from multiple points in space, you can extract additional information from the medium being sampled.

Figure 1. A sensor array detecting two distant electromagnetic sources at unknown angles.

Figure 1. A sensor array detecting two distant electromagnetic sources at unknown angles.

Figure 1. A sensor array detecting two distant electromagnetic sources at unknown angles.

Our application uses a sensor array to determine the direction of arrival (DOA) of multiple distant electromagnetic sources, such as radio beacons and radar transmitters. In this scenario, we will attempt to estimate the angles θ1 and θ2 of the two sources relative to the direction the sensor array is pointing.

Reviewing Data Items and Operations

We will use a simple fast Fourier transform (FFT)-based technique to estimate the DOA of the sources. This technique can be broken down into parts and implemented as a collection of operations. A small number of utility operations will be implemented to help simplify the development work. For example, we must:

Having identified the data we need to represent and the activities we need to perform, we can represent the data with class properties and the activities with class methods.

Representing Data with Class Properties

We begin by defining a class to describe the sensor array. This initial representation contains only the data items and represents them as class properties.

You define a class in MATLAB with a class definition file, which begins with the classdef keyword and is terminated by the end keyword. Within the class definition block, additional keyword blocks will describe different aspects of the class, such as class properties and class methods. The definition file shown in Figure 2 describes a class sads (sensor array data set) with all the data items that we need to represent listed in a properties block.

Figure 2. Class definition file sads.m with properties.

Figure 2. Class definition file sads.m with properties.

Figure 2. Class definition file sads.m with properties.