Create a Simple 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.
Create an object of the class using the class name:
Initially, the property value is empty.
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 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
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
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:
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 .
For information on overloading functions, see Overload Functions in Class Definitions.
For information on overloading operators, see Operator Overloading.
Here is the definition after adding the features discussed in this topic:
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).