Creating simple classes in Matlab

Posted by ineedhelp on Tue, 22 Feb 2022 02:07:11 +0100

catalogue

Create a simple class

Design class

create object

Access properties

Call method

Add constructor

Method Vectorization

overloaded function

BasicClass code list

Create a simple class

Design class

The basic purpose of class is to define the object that encapsulates the data and the operations to be performed on the data. For example, BasicClass defines a property and two methods to perform operations on the data in the property:

  • Value - this property contains numeric data stored in the class object

  • roundOff - this method rounds the attribute value to two decimal places

  • multiplyBy - this method multiplies the property value by the specified value

The following is the definition of BasicClass:

classdef BasicClass
   properties
      Value {mustBeNumeric}
   end
   methods
      function r = roundOff(obj)
         r = round([obj.Value],2);
      end
      function r = multiplyBy(obj,n)
         r = [obj.Value] * n;
      end
   end
end

To use a class:

  • Save the class definition in a file with the same name as the class m file.

  • Create an object of this class.

  • Access properties to assign data to properties.

  • Call methods to perform operations on the data.

create object

Create an object of the class using the class name:

a = BasicClass
a = 

  BasicClass with properties:

    Value: []

The property value is initially empty.

Access properties

Assign a Value to the Value attribute by adding a dot and an attribute name to the object variable:

a.Value = pi/3;

To return an attribute value, use the dot notation without an assignment:

a.Value
ans =

    1.0472

Call method

Call the roundOff method on object a:

roundOff(a)
ans =

    1.0500

Pass the object as the first parameter to the method that accepts multiple parameters. Here, take the call of myltiplyBy method as an example:

multiplyBy(a,3)
ans =

    3.1416

You can also use dot notation to call methods:

a.multiplyBy(3)

When using dot notation, it is not necessary to explicitly pass objects as parameters. This notation places objects to the left of the method name, separated by dots.

Add constructor

A class can define a special method to create a class object, called a constructor. You can use constructor methods to pass parameters to constructors to assign values to properties. The Value property of BasicClass uses the mustBeNumeric function to limit its possible values.

The following is the constructor of the {BasicClass} class. If you call the constructor with an input parameter, this parameter is assigned to the Value property. If the constructor is called without input parameters, the Value property takes its default Value, which is empty ([]).

methods        
    function obj = BasicClass(val)
        if nargin == 1
            obj.Value = val;
        end
    end
end

By adding this constructor to the class definition, you can create an object and set the property value in one step:

a = BasicClass(pi/3)
a = 

  BasicClass with properties:

    Value: 1.0472

Constructors can also perform other operations related to creating class objects.

Method Vectorization

        MATLAB ® Support operational vectorization. For example, you can add numbers to a vector:

[1 2 3] + 2

ans =

     3     4     5

MATLAB adds the number 2 to each element in the array [1, 2, 3]. To vectorize arithmetic operator methods, set obj The value property reference is enclosed in square brackets.

[obj.Value] + 2

This syntax enables methods to handle arrays of objects. For example, create an array of objects using index assignment.

obj(1) = BasicClass(2.7984);
obj(2) = BasicClass(sin(pi/3));
obj(3) = BasicClass(7);

Then the following expression:

[obj.Value] + 2

Equivalent to the following expression:

[obj(1).Value obj(2).Value obj(3).Value] + 2

Since the roundOff method is vectorized, it can perform operations on arrays:

roundOff(obj)
ans =

    2.8000    0.8700    7.0000

overloaded function

Class can realize existing functions by defining methods with the same name as existing MATLAB functions, such as addition. For example, suppose you want to add two BasicClass objects. This usually means adding the values of the Value property of each object.

The following is the overloaded version of MATLAB # plus # function. It defines the addition of the {BasicClass} class as the addition of attribute values:

method
   function r = plus(o1,o2)
      r = [o1.Value] + [o2.Value];
   end
end

By implementing the method named plus, you can use the "+" operator on the object of BasicClass.

a = BasicClass(pi/3);
b = BasicClass(pi/4);
a + b
ans =

    1.8326

The vectorization plus method allows you to perform operations on an array of objects.

a = BasicClass(pi/3);
b = BasicClass(pi/4);
c = BasicClass(pi/2);
ar = [a b];
ar + c
ans =

    2.6180    2.3562

BasicClass code list

The following is the BasicClass definition after adding the functions discussed in this topic:

classdef BasicClass
    properties
        Value {mustBeNumeric}
    end
    methods
        function obj = BasicClass(val)
            if nargin == 1
                obj.Value = val;
            end
        end
        function r = roundOff(obj)
            r = round([obj.Value],2);
        end
        function r = multiplyBy(obj,n)
            r = [obj.Value] * n;
        end
        function r = plus(o1,o2)
            r = [o1.Value] + [o2.Value];
        end
    end
end

Topics: MATLAB