Creating a Simple Class - MATLAB & Simulink (original) (raw)

Design Class

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

Start a class definition with a classdef_`ClassName`_...end block, and then define the class properties and methods inside that block. Here is the definition ofBasicClass:

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

For a summary of class syntax, see classdef.

To use the class:

Create Object

Create an object of the class using the class name:

a =

BasicClass with properties:

Value: []

Initially, the property value is empty.

Access Properties

Assign a value to the Value property using the object variable and a dot before the property name:

To return a property value, use dot notation without the assignment:

For information on class properties, see Property Syntax.

Call Methods

Call the roundOff method on object a:

Pass the object as the first argument to a method that takes multiple arguments, as in this call to the multiplyBy method:

You can also call a method using dot notation:

Passing the object as an explicit argument is not necessary when using dot notation. The notation uses the object to the left of the dot.

For information on class methods, see Method Syntax.

Add Constructor

Classes can define a special method to create objects of the class, called a constructor. Constructor methods enable you to pass arguments to the constructor, which you can assign as property values. The BasicClass Value property restricts its possible values using the mustBeNumeric function.

Here is a constructor for the BasicClass class. When you call the constructor with an input argument, it is assigned to the Value property. If you call the constructor without an input argument, the Value property has a default value of 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 with properties:

Value: 1.0472

The constructor can perform other operations related to creating objects of the class.

For information on constructors, see Class Constructor Methods.

Vectorize Methods

MATLAB® enables you to vectorize operations. For example, you can add a number to a vector:

MATLAB adds the number 2 to each of the elements in the array [1 2 3]. To vectorize the arithmetic operator methods, enclose the obj.Value property reference in brackets.

This syntax enables the method to work with arrays of objects. For example, create an object array using indexed assignment.

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

These two expressions are equivalent.

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

The roundOff method is vectorized because the property reference is enclosed in brackets.

r = round([obj.Value],2);

BecauseroundOff is vectorized, it can operate on arrays.

ans =

2.8000    0.8700    7.0000

Overload Functions

Classes can implement existing functionality, such as addition, by defining a method with the same name as the existing MATLAB function. For example, suppose that you want to add two BasicClass objects. It makes sense to add the values of the Value properties of each object.

Here is an overloaded version of the MATLABplus function. It defines addition for the BasicClass class as adding the property values:

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

By implementing a method called plus, you can use the “+” operator with objects of BasicClass.

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

By vectorizing the plus method, you can operate on object arrays.

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

For information on overloading functions, see Overload Functions in Class Definitions.

For information on overloading operators, see Operator Overloading.

BasicClass Code Listing

Here is the BasicClass definition after adding the features 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