What is Object-Oriented Programming in MATLAB? - MATLAB
Video length is 9:30

What is Object-Oriented Programming in MATLAB?

Object-oriented programming is a design approach that enables you to programmatically define structures called objects that combine data (properties) together with functions that operate on that data (methods). In MATLAB®, you can create objects that model the behavior of devices and systems in the real world. Those objects can then be used as building blocks in applications used to simulate and analyze complex systems. This video provides an overview of object-oriented programming and guides you through an example that highlights the features and capabilities of MATLAB that support this programming design approach.

Published: 1 Oct 2024

Objects, classes, properties, methods, these are some of the most important concepts in object-oriented programming. But sometimes they can feel hard to wrap your head around. So let's demystify object-oriented programming and how we can leverage it to our advantage in MATLAB.

Today, we'll walk through an overview of object-oriented programming. And then we'll jump into an example to highlight MATLAB's features and capabilities. Hopefully, by the end of this video, we'll understand the basics of what is object-oriented programming in MATLAB.

Object-oriented programming is a design approach that enables us to programmatically create structures, called objects, that combine data, called properties, together with functions that operate on that data, called methods. In MATLAB, objects model the behavior of devices and systems in the real world. Users can build class definition files, specify properties and methods and more to achieve their object-oriented goals.

To kick off programming with objects in MATLAB, first, we need a class definition file. All we need to do to make that is to go to the MATLAB toolbar and click New, Class. Then name the class what you'd like. It's in this unassuming file that our object-oriented journey begins. I've actually cooked up a class definition ahead of time. Let's take a quick tour.

A MATLAB class definition file contains a blueprint, or a set of instructions, used to build a specific type of object. Class definitions start with the classdef keyword, followed by the name of the class and have two important components we'll discuss today. Properties blocks define the properties that store data for each of the objects of the class. Methods blocks contain a set of functions that define the operations that can be performed on each object of the class.

While all this talk of classes is well and good, where are the objects? Well, to create an object, we just call the class definition file and assign the output, or object, to a variable. Now that our way around a class definition file, let's consider an example where we can see these concepts in action, orienteering with object-oriented programming.

Orienteering is a sport in which participants use a map to find their way through the wilderness to a series of waypoints as quickly as possible. With many different participants trying to find different waypoints, everyone needs a way to record which waypoints they found and when. To do this, participants carry an electronic stick. When they reach a waypoint, the ID stick records the visit to that waypoint.

This example is featured in the MATLAB Object-Oriented Programming Onramp tutorial. And we'll be using a simplified version of it today. We need to create a system in which participants in an orienteering competition can register for a specific course and then log their progress using ID sticks.

Today, we'll be working with two interacting classes-- course, which describes a specific course, and idstick, which will track participants progress through the course. By the time we're done, we'll have made a basic orienteering system, all while learning the ins and outs of object-oriented programming. Let's dive in.

The properties defined in the classdef file can be any data type and contain the necessary information about that object. In our example, the classdef file, course.m, contains the name of the course, express as a string, a level classifying its intensity, also a string, and a series of waypoints along the course, expressed as an array of doubles. These three properties together comprise a full description of an orienteering course.

The methods of a class contain functions for the operations that can be carried out on objects of that class. In our example, we have the method checkWaypoint, which is used to track when a participant visits a waypoint as they navigate the course. When we run this function, we see a waypoint being logged by the method.

So this class definition contains the description of an orienteering course, including a list of waypoints and a method that tracks participants and logs their visits to waypoints throughout the course. We can reference these properties and methods with dot notation. And we can currently modify their values as we please. But we'll talk later about how we can prevent users from modifying property values.

Now, let's create an object based on this course classdef file. To create an object of a class, we use a constructor method. All MATLAB classes have a default constructor method. And in this case, the default creates an object and sets its properties to empty doubles.

We can also create our own custom constructor method. One common reason why we do this is that it enables us to initialize the properties of the object when we create it. A constructor method is a special method in the class, whose name is the same as the class and whose output is an instance of the class or an object.

Let's make our constructor take in three inputs and assign them to the three properties for this class-- name, level, and waypoints. An important addition we need to make to this constructor is to build in the capability for it to return an object with default property values when it is called with no inputs, which can happen automatically sometimes when it's referenced elsewhere. We can now store the course object in idstick as one of its properties. Now, we have a robust constructor method for our course class, which will aid our orienteering endeavors by allowing us to quickly fill in course information and reference it elsewhere, like in idstick.

What if we need to create class definitions for related objects? So instead of just having an ordered course where participants follow a set path of waypoints as quickly as possible, we could also have a scored course where participants can go to waypoints in any order they wish in a set amount of time, with different waypoints being worth different amounts of points. This is where superclasses and subclasses come in. These are great examples of how object-oriented programming can make your code more robust, structured, and reusable. Let's see how they work.

We start off by identifying the aspects of the two types of courses that are shared. And then, we identify the aspects that are unique to each course type. The shared aspects go into the superclass, course. And the unique aspects are placed in the two new subclasses, ordered course and scored course.

We don't need to change anything in the syntax of course.m for it to become a superclass. And all we need to do to make the other two subclasses reference course.m is to add less than course to the first line of their classdef file. Now we have two distinct types of courses based on a common course class. This concept is called inheritance.

We want these orienteering classes we've made to be easy to use for any user, not just ourselves. To accomplish this, we need to apply some restrictions so that a user doesn't accidentally corrupt the code or attempt to use incorrect values for the properties. Property validation can enforce what data can be assigned to a property, a process in which we lay out specific dimensions and data types, which any input must conform to.

Then, we can go further by restricting whether a user of the class can write or read property values. We can apply a variety of restrictions to our properties, like SetAxis, which controls whether a user can set an object's properties, GetAccess, which controls whether a user can retrieve the properties of an object, or Access, which controls both at the same time. All these attributes are set to public by default, but we can set them to private, thus barring users from either changing or seeing the values of these properties.

What if we wanted to make only some of the properties invisible to a user? In idstick, serial number and status are both properties we don't need the user to see or edit. And we definitely don't want the user to accidentally corrupt them.

We would move these properties to a new properties block, then add the Access equals private attribute to that block. This prevents a user from editing or even viewing those properties as they use the classes. Now, we're free to share our user friendly and secure classes with the world.

And there you have it. We've gone from the humble beginnings of a simple class definition all the way to leveraging our definition files to create robust and shareable objects, which goes to show how you can make your object-oriented dreams become reality with MATLAB. But your journey doesn't stop here. We've covered enough today to get you started with object-oriented programming. But MATLAB supports a wide array of more advanced capabilities and features for you to explore.

For more information, check out the free MATLAB Object-Oriented Programming Onramp tutorial, which builds on the topics and the example we used today, as well as documentation on MATLAB's extensive object-oriented programming capabilities, all linked in the description. Thanks for watching, and I'll see you in another video.