Methods are functions that implement the operations performed on objects of a class. Methods, along with other class members support the concept of encapsulation—class instances contain data in properties and class methods operate on that data. This design allows the internal workings of classes to be hidden from code outside of the class, and thereby enabling the class implementation to change without affecting code that is external to the class.
Methods have access to private members of their class including other methods and properties. This encapsulation enables you to hide data and create special interfaces that must be used to access the data stored in objects.
For an example to get started writing classes, see Create a Simple Class
For sample code and syntax, see Methods and Functions
For a discussion of how to create classes that modify standard MATLAB® behavior, see Methods That Modify Default Behavior .
For information on the use of @ and path directors and packages to organize your class files, see Class Files and Folders
For the syntax to use when defining classes in more than one file, see Methods in Separate Files
There are specialized kinds of methods that perform certain functions or behave in particular ways:
Ordinary methods are functions that act on one or more objects and return some new object or some computed value. These methods are like ordinary MATLAB functions that cannot modify input arguments. Ordinary methods enable classes to implement arithmetic operators and computational functions. These methods require an object of the class on which to operate. See Ordinary Methods.
Constructor methods are specialized methods that create objects of the class. A constructor method must have the same name as the class and typically initializes property values with data obtained from input arguments. The class constructor method must declare at least one output argument, which is the object being constructed. The first output is always the object being constructed. See Class Constructor Methods
Destructor methods are called automatically when the object is destroyed, for example if you call
delete(object) or there are no longer any references to the object. See Handle Class Destructor
Property access methods enable a class to define code to execute whenever a property value is queried or set. See Property Access Methods
Static methods are functions that are associated with a class, but do not necessarily operate on class objects. These methods do not require an instance of the class to be referenced during invocation of the method, but typically perform operations in a way specific to the class. See Static Methods
Conversion methods are overloaded constructor methods from other classes that enable your class to convert its own objects to the class of the overloaded constructor. For example, if your class implements a
double method, then this method is called instead of the double class constructor to convert your class object to a MATLAB double object. See Object Converters for more information.
Abstract methods define a class that cannot be instantiated itself, but serves as a way to define a common interface used by numerous subclasses. Classes that contain abstract methods are often referred to as interfaces. See Abstract Classes for more information and examples.
The name of a function that implements a method can contain dots (for example,
set.PropertyName) only if the method is one of the following:
You cannot define property access or conversion methods as local functions, nested functions, or separately in their own files. Class constructors and package-scoped functions must use the unqualified name in the function definition; do not include the package name in the function definition statement.