Matlab Class Assignment Operator Definition

Create a Simple Class

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, defines a property and two methods that operate on the data in that property:

  • — Property that contains the data stored in an object of the class

  • — Method that rounds the value of the property to two decimal places

  • — Method that multiplies the value of the property by the specified number

Here is the definition of :

For a summary of class syntax, see .

To use the class:

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

  • Create an object of the class.

  • Access the properties to assign data.

  • Call methods to perform operation on the data.

classdef BasicClass properties Value endmethodsfunction r = roundOff(obj) r = round([obj.Value],2); endfunction r = multiplyBy(obj,n) r = [obj.Value] * n; endendend

Create Object

Create an object of the class using the class name:

Initially, the property value is empty.

a = BasicClass with properties: Value: []

Access Properties

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

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

For information on class properties, see Properties.

Call Methods

Call the method on object :

Pass the object as the first argument to a method that takes multiple arguments:

You can also call a method using dot notation:

It is not necessary to pass the object explicitly as an argument when using dot notation. The notation uses the object to the left of the method name.

For information on class methods, see Methods and Functions

Add Constructor

Classes can define a special method to create objects, called a constructor. Constructor methods enable you to pass arguments to the constructor, and to validate and assign property values. Here is a constructor for the class:

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

This constructor also performs type checking on the input argument. For example:

For information on constructors, see Class Constructor Methods

methodsfunction obj = BasicClass(val) if nargin > 0 if isnumeric(val) obj.Value = val; else error('Value must be numeric') endendendend
a = BasicClass with properties: Value: 1.0472
a = BasicClass('A character array')
Error using BasicClass (line 11)Value must be numeric

Vectorize Methods

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

MATLAB adds the number to each of the elements in the array . To vectorize the arithmetic operator methods, enclose the property reference in brackets, where is an object array.

This syntax enables the method to work with arrays of object. For example, given objects , , and :

By using vector notation, can be an array:

[a1.Value,a2.Value,a3.Value] + 2
a(1) = BasicClass(2.7984); a(2) = BasicClass(sin(pi/3)); a(3) = BasicClass(7); roundOff(a)
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 objects. It makes sense to add the values of the properties of each object.

Here is an overload of the MATLAB function. It defines addition for this class as adding the property values:

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

Related Information

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

For information on overloading operators, see Operator Overloading.

methodfunction r = plus(o1,o2) r = [o1.Value] + [o2.Value]; endend
a = BasicClass(pi/3); b = BasicClass(pi/4); a + b

Code Listing

Here is the definition after adding the features discussed in this topic:

classdef BasicClass properties Value endmethodsfunction obj = BasicClass(val) if nargin == 1 if isnumeric(val) obj.Value = val; else error('Value must be numeric') endendendfunction r = roundOff(obj) r = round([obj.Value],2); endfunction r = multiplyBy(obj,n) r = [obj.Value] * n; endfunction r = plus(o1,o2) r = o1.Value + o2.Value; endendend

Related Topics

  • You can generate code for entry-point MATLAB functions that use classes, but you cannot generate code directly for a MATLAB class.

    For example, if is a class definition, you cannot generate code by executing:

  • A handle class object cannot be an entry-point function input or output.

  • A value class object can be an entry-point function input or output. However, if a value class object contains a handle class object, then the value class object cannot be an entry-point function input or output. A handle class object cannot be an entry-point function input or output.

  • Code generation does not support global variables that are classes.

  • You cannot use classes for Simulink® signals, parameters, or data store memory.

  • Code generation does not support assigning an object of a value class into a nontunable property. For example, is invalid when is a nontunable property and is an object based on a value class.

  • You cannot use to declare a class or method as extrinsic.

  • You cannot pass a MATLAB class to . You can pass class properties to .

  • If a property has a get method, a set method, or validators, or is a System object™ property with certain attributes, then you cannot pass the property by reference to an external function. See Passing By Reference Not Supported for Some Properties.

  • If you use classes in code in the MATLAB Function block, you cannot use the debugger to view class information.

  • The function does not support MATLAB classes as inputs.

  • If an object has duplicate property names and the code generator tries to constant-fold the object, code generation can fail. The code generator constant-folds an object when it is used with , or when it is an input to or output from a constant-folded extrinsic function.

    Duplicate property names occur in an object of a subclass in these situations:

    • The subclass has a property with the same name as a property of the superclass.

    • The subclass derives from multiple superclasses that use the same name for a property.

    For information about when MATLAB allows duplicate property names, see Subclassing Multiple Classes (MATLAB).

  • 0 thoughts on “Matlab Class Assignment Operator Definition

    Leave a Reply

    Your email address will not be published. Required fields are marked *