EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Class
 

Matlab Class

Priya Pedamkar
Article byPriya Pedamkar

Updated March 4, 2023

Matlab Class

 

 

Introduction to Matlab Class

Classes are used in object-oriented programming to create many objects which are similar to each other and share common characteristics, like dog breeds, geometric figures, employee records, nationality, etc. For example, the car models; we can be assured that cars of the same kind of model are similar & it can help us in creating a blueprint for all these cars. In this topic, we are going to learn about Matlab Class.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Description

In MATLAB we use the class to define an object in the following terms:

1. The data that the object contains; referred to as properties

Types of properties:

  • AbortSet
  • Abstract
  • Access
  • Constant
  • Dependent
  • GetAccess

2. The methods or functions implemented on data

Types of methods:

  • Ordinary
  • Destructor
  • Constructor
  • Static
  • Abstract
  • Conversion

Let us now learn how to create a class in MATLAB. For our understanding, we will create a class that will be used to calculate the area of a rectangle.

Syntax

Here are the following syntax mention below

Step #1 – Defining the Class

In MATLAB, we use the ‘classdef ‘method to define a class. Using this method, we define the name of our class, properties, or data values of an object in our class, and methods or functions which we want to be implemented on the objects.

For our class ‘Rectangle’, ‘rectangle’ will be an object. To define our object ‘rectangle’, we will be defining the following 2 parameters:

1. Properties or data values for our object. Properties for ‘rectangle’ object will include:

  • Color of rectangle
  • Height of rectangle
  • Width of rectangle

2. Action to be performed on the properties or data in the above step. In our example, action will compute the area

Code:

classdef Rectangle
[Defining the class ‘Rectangle’ using ‘classdef’ method] properties
[Defining the properties of the class ‘Rectangle’] Colour
[This will define the colour of the rectangle] Height
[This will define height of the rectangle] Width
[This will define width of the rectangle] end
end

For a class, we can check the properties using ‘properties’ command as below:

properties (Rectangle)

Please note that the class created must be stored in a file with ‘.m’ extension and the name of this file must be the same as the name of the class. So, for our example, the name of the file will be ‘Rectangle.m’

Input:

class def Rectangle
properties
Colour
Height
Width
end
end

Output:

Matlab Class output 1

As we can see in the output, we have obtained the properties of our class ‘Rectangle’.

Please observe the name of the file in the Input. As stated before, it is the same as the name of the class created by us. 

Step #2

Next, we will define the function to be implemented on the data collected above:

Function to be implemented is defined using a ‘methods’ block.

Code:

methods
[Initializing the ‘methods’ block] function Area = getArea (newRectangle)
[Using the ‘getArea’ function to calculate the area of the rectangle and creating the object ‘newRectangle’] Area = newRectangle.Height * newRectangle.Width;
[Using our newly created object to access the Height and Width of the Rectangle from the properties] end
end

Step #3

Next, we will create an object by specifying the object as a member of the class.

For this, we will create an instance of a particular rectangle object. An object can also be created using constructors. Let us learn both techniques one by one.

Specifying the object as a member of the class:

Code:

RectObject = Rectangle
[‘RectObject’ Instance is created for a particular rectangle object]

Output:

Matlab Class output 2

As we can see in the output, an object is created with properties the same as the properties of the class Rectangle. Since we have not passed any values yet, the object is created with empty values. 

Step #4

Next, we will assign values to the object RectObject:

Code:

RectObject.Width = 5;
[Assigning the value to the ‘Width’ property] RectObject.Height = 7;
[Assigning the value to the ‘Height property] RectObject.Colour = 'green';
[Assigning the value to the ‘Colour property] RectObject
[To display the properties of object]

Output:

Matlab Class output 3

As we can see in the output, all the values are now assigned to the object RectObject.

Using the Constructor function to create an object of the class:

Code:

function newRectangle = Rectangle (colour, width, height)
[Assigning class name with properties to the name of the constructor] if nargin ==3
newRectangle.Colour = colour;
newRectangle.Width = width;
newRectangle.Height = height;
end
end
RectObject = Rectangle('green', 5, 7)

[Please note that, in this technique, the values for Colour, Width and Height are assigned by specifying all as the input arguments of constructor function]

Output:

output 4

As we can see in the output, all the properties are assigned to the object RectObject.

Step #5

Finally, to calculate the area of the rectangle simply call ‘getArea’ method of the class ‘Rectangle’ using the object created:

Code:

getArea(RectObject)
[Calling the ‘getArea’ method using the object ‘RectObject’]

Output:

output 5

Conclusion – Matlab Class

We use Class to define an instance of an object that carries data & the methods to be implemented on that data. Steps to using a class:

  1. Use the .m file to save the class
  2. The name of the file must be the same as that of the class
  3. Create an object
  4. Access the properties of the class to assign data
  5. Call the methods defined to perform operations on the data.

Recommended Articles

This is a guide to Matlab Class. Here we discuss the introduction to Matlab Class along with the description, steps and respective examples. You may also have a look at the following articles to learn more –

  1. Matlab loglog()
  2. Matlab rref
  3. Bar Plot Matlab
  4. MATLAB cylinder()

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW