Classes and Object in MATLAB
Last Updated :
09 May, 2021
A class is a blueprint that defines the variables and the methods which provide a commonly shared basis for its corresponding objects. It defines an object that encapsulates data and the operations performed on that data. classdef is a keyword used to define MATLAB classes.
Syntax to define a class:
classdef (Attributes) ClassName < SuperclassName
properties (Attributes)
PropertyName
PropertyName size class {validation functions}
end
methods (Attributes)
function obj = methodName(obj,arg2,...)
...
end
end
events (Attributes)
EventName
end
end
Components of MATLAB class:
MATLAB class has three major components (which are MATLAB functions used to query the respective class members for a given object or class name):
- 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.
- Events blocks: Define messages that an object will send to other parts of an application when something changes in that object.
Here is an example of a MATLAB class,
SimpleClass defines a property and two methods that operate on the data in that property:
Matlab
classdef SimpleClass
properties
Value {mustBeNumeric}
end
methods
function R = roundOff(object)
R = round([object.Value],2);
end
function R = DivideBy(object,n)
R = [object.Value] / n;
end
end
end
In the above example:
- Value - Property that holds the numeric data stored in an object of the class.
- roundOff - Method that roundoff the value of the property to two decimal places.
- DivideBy - Method that multiplies the value of the property by the specified number.
To use the class:
- Save the class definition with the same name as the class, keep the extension of the file as (.m).
- Create an object of the class.
- Access the properties to assign data and call methods to perform an operation on the data.
Objects
Similar to any other programming language, objects in MATLAB are instances of their respective classes. In MATLAB, objects of a class can be created in two ways:
Create an object of the class using the class name
Create object: Below is the script to create an object of the above class.
Matlab
a =
SimpleClass with properties:
Value: []
Initially, the property value is empty.
Access Properties: Using the object variable and a dot before the property name, we can assign value to the Value property :
Matlab
Property value is returned if we use dot notation without the assignment:
Matlab
Output:
ans =
3.1416
Call Methods: Call the roundOff method on object a:
Matlab
Output:
ans =
3.1400
Pass the object as the first argument to a method that takes multiple arguments, as in this call to the DivideBy method:
Matlab
Output:
ans =
1.0472
The method can also be called using dot notation:
Matlab
Output:
ans =
1.0472
It is not mandatory to pass the object explicitly as an argument when using dot notation.
Create object using Constructor
We can also create an object(or an array of objects) using a class constructor. Constructor methods enable us to pass arguments to the constructor, which you can assign as property values. Â The mustBeNumeric function restricts the possible values of SimpleClass Value property. Constructor is called like any MATLAB function. You can access object properties and object methods are called just like ordinary MATLAB functions.
Here is a constructor for the SimpleClass class. When the constructor is called with an input argument, it is assigned to the Value property, but if it is called without an input argument, it has a default value of empty ([]).
Matlab
classdef SimpleClass
properties
Value {mustBeNumeric}
end
methods
function obj = SimpleClass(val)
if nargin == 1
obj.Value = val;
end
end
end
end
We can create an object and set the property value in one step by adding a constructor to the class definition:
Matlab
Output:
a =
SimpleClass with properties:
Value: 1.0472
The constructor is also used in performing operations related to creating objects of the class.
Note: MATLAB objects have unique features relative to other languages. For example, you can modify a class at any time, and the objects of that class will update immediately. Managing the lifecycle of objects in MATLAB is done without requiring any explicit memory allocation or deallocation.
A snap of object creation of class SimpleClass in MATLAB:
Similar Reads
Copy Objects in MATLAB In MATLAB, there are two kinds of objects - handles and values. The value objects are ordinary MATLAB objects which behave normally to copy operations. It means that when a value object is copied, the new copies of that object are completely independent of the original one i.e., if the original's va
2 min read
Understanding Classes and Objects in Java The term Object-Oriented explains the concept of organizing the software as a combination of different types of objects that incorporate both data and behavior. Hence, Object-oriented programming(OOPs) is a programming model, that simplifies software development and maintenance by providing some rul
10 min read
Classes & Objects in Objective-C Objective-C is an object-oriented programming language that has been used for developing software applications for various Apple platforms, such as iOS, macOS, watchOS, and tvOS. Classes and objects are the fundamental building blocks of object-oriented programming in Objective-C. A class is a bluep
8 min read
Characters and Strings in MATLAB In this article, we shall see how to deal with characters and Strings in MATLAB. A data type is an attribute/keyword that specifies the type of data that the object can hold: numeric data or text data. By default, MATLAB stores all numeric variables as double-precision floating-point values. Additio
4 min read
User-Defined Classes in MATLAB In this article, we will understand User-defined Classes in MATLAB. Object-Oriented Programming:The object-oriented program design involves: Identify the components of the system or application that you want to build.Analyzing and identifying patterns to determine what components are used repeatedly
3 min read
Scripts and Functions in MATLAB In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following: ScriptLive ScriptFunction only fileClass fileNow only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we sh
2 min read
Class Constructor Methods in MATLAB Class constructors are special methods in MATLAB that are used to create an instance of a class. In other words, they are used to initialize the properties of an object when it is created. Class constructors typically have the same name as the class itself, and they are defined as methods within the
4 min read
GUI Based Tables in MATLAB GUI tables in MATLAB are graphical user interface that allow users to display and manipulate tabular data.They are used to create interactive applications that require data to be displayed in a table format. GUI tables in MATLAB typically consist of columns and rows , with each column representing a
3 min read
Basic Object Model in Excel VBA VBA stands for visual basic for application. Excel VBA is an object-based programming language, it is used while recording a macro i.e., it uses the idea of Encapsulation and stores the state (data) and operation (functions) inside the object. Excel objects are arranged in a hierarchy that governs t
3 min read
Cell Arrays in MATLAB In MATLAB, cell arrays are a type of arrays which stores elements of different data types and sizes in different cells, or one could say that cell arrays allow users to store heterogeneous data into a single array. For example, let us create a cell array which holds the name and age of a person. Exa
2 min read