Open In App

User-Defined Classes in MATLAB

Last Updated : 15 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 or share characteristics.
  • Classifying components based on similarities and differences

The basic elements of Object-oriented programming are: Objects, Classes, Inheritance, Abstraction, Polymorphism, and Encapsulation. In which the object and the class are the two basic principles of Object-oriented programming. 

Classes and Objects:

An object is a real-world entity that we see around us. Every object has its properties and method (characteristics and behavior). For example, consider a Car. A Car is an object that has properties or characteristics like color, size, model name. And methods such as drive, apply horn, apply break, and so on. 

A Class is a blueprint of the objects or the collection of objects. We know objects contain some characteristics and behavior also known as Attribute and Method. With the use of Class, we can use attributes and methods. User-defined classes are classes that a user defines based on his needs. Here is the syntax to create a user-defined class:

Syntax:

classdef userdefinedClassName
   properties
   end
   methods
   end
end

classdef is the keyword that is used to define the user-defined class in Matlab. The important point to note is the filename and the class name should match. In the below example, we shall create a new file named geeksforgeeks.m and define the class with the name geeksforgeeks. 

Example 1: 

Matlab




% MATLAB code for user-defined class 
classdef geeksforgeeks
  properties
    courses
    practice_session
    contest
  end
  methods
  end
end


Output:

=> <object geeksforgeeks>
courses: []
practice_session: []
contest: []

Example 2:

Matlab




% MATLAB code for user-defined class 
classdef geeks
  properties
    courses
    practice_session
  end
  methods
  end
end


Output:

invalid classdef definition, the class name must match the filename

In the above example courses, practice_session are the class attributes that are initialized with the class properties. 

Creating an Object of Class:

Once we have the class defined, we can access the class attributes by creating an instance or object of the class. Here is the syntax to create an object and use its property. 

Syntax:

object_name = classname
object_name.property

Example 3:

Matlab




% MATLAB code for create object in class
geeks = geeksforgeeks  //here geeks is an object
//dot notation to access the attributes
geeks.practice_session = "Microsoft Interview";
display(geeks.practice_session);
display(geeks.courses);


Output:

Microsoft Interview
[](0x0)

Example 4:

Matlab




% MATLAB code for object creation
geeks.courses = "DSA Preparation";
display(geeks.courses);


Output:

DSA Preparation

Creating  Constructor:

So far we have discussed how to deal with properties, now we shall see how to work with methods in user-defined classes. Methods are used to change the behavior of the class attribute which is defined as the class function. Constructor is described as the method name with the exact name of the class name. For example, in the below example, anime() is a Constructor

Create a new file and name the class name with the file name. 

Example 5:

Matlab




classdef anime
  properties(constant)
    name
  end
  methods
    function obj = anime()
      obj.name = "One Piece"
    end
  end
end


Output: 

ans= One Piece


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads