Documentation |
On this page… |
---|
How to Customizing Class Behavior Which Methods Control Which Behaviors Overloading and Overriding Functions and Methods numel, subsref, and subsasgn — Considerations for Overloading |
There are functions that MATLAB^{®} calls implicitly when you perform certain actions with objects. For example, a statement like [B(1);A(3)] involves indexed reference and vertical concatenation.
You can change how user-defined objects behave by defining methods that control specific behaviors. To change a behavior, implement the appropriate method with the name and signature of the MATLAB function.
The following table lists the methods to implement for your class and describes the behaviors that they control.
Class Method to Implement | Description |
---|---|
Concatenating Objects | |
cat, horzcat, and vertcat | Customize behavior when concatenation objects |
Creating Empty Arrays | |
Create empty arrays of the specified class. See Creating Empty Arrays | |
Displaying Objects | |
Called when you enter disp(obj) on the command line Called when statements are not terminated by semicolons. disp is often used to implement display methods. | |
Converting Objects to Other Classes | |
Convert an object to a MATLAB built-in class See The DocPolynom to Character Converter and The DocPolynom to Double Converter | |
Indexing Objects | |
Enables you to create nonstandard indexed reference and indexed assignment | |
Supports end syntax in indexing expressions using an object; e.g., A(1:end) | |
Determine the number of elements in an array | |
Determine the dimensions in an array | |
Support using an object in indexing expressions | |
Saving and Loading Objects | |
loadobj and saveobj | Customize behavior when loading and saving objects |
Overloading and overriding are terms that describe techniques for customizing class behavior. Here is how we use these terms in MATLAB.
Overloading means that there is more than one function or method having the same name within the same scope. MATLAB dispatches to a particular function or method based on the dominant argument. For example, the timeseries class overloads the MATLAB plot function. When you call plot with a timeseries object as an input argument, MATLAB calls the timeseries class method named plot.
Overriding means redefining a method inherited from a superclass. MATLAB dispatches to the most specific version of the method. That is, if the dominant argument is an instance of the subclass, then MATLAB calls the subclass method.
Use the InferiorClasses attribute to control class precedence. See Class Attributes for more information.
Many MATLAB functions depend on the behavior of other functions, like size and numel. Therefore, you must be careful to ensure that what is returned by an overloaded version of these functions is a correct and accurate representation of the size of an object array.
You might need to define a numel method to compensate when your class defines a specialized version of size.
subsref uses the value returned by numel to compute the number of expected output arguments returned by subsref from subscripted reference (i.e., nargout).
Similarly, subsasgn uses numel to compute the expected number of input arguments to be assigned using subsasgn (i.e., nargin).
MATLAB determines the value of nargin for an overloaded subsasgn function from the value returned by numel, plus two (one for the variable to which you are making an assignment and one for the struct array of subscripts).
If MATLAB produces errors when calling your class's overloaded subsref or subsagn methods because nargout is wrong for subsref or nargin is wrong for subsasgn, then you need to overload numel to return a value that is consistent with your implementation of these indexing functions.
See Understanding size and numel and Indexed Reference and Assignment for more information on implementing subsref and subsagn methods.
When resolving an indexed reference or assignment, MATLAB calls numel to determine the number of outputs that are expected from subsref or the number of inputs required by subsasgn.
To support {} indexing, you need to overload numel to accept a variable number of input index values in addition to the object array. Define the overloaded numel with this syntax:
function n = numel(A,varargin) ... end
where varargin represent indices into array A.
MATLAB uses the value returned by numel to determine:
How many outputs to return from subsref in the case of indexed reference
How many inputs to pass to subsasgn in the case of indexed assignment
When overloading subsref, define the method to return multiple values for the indexed reference using varargout:
function [varargout] = subsref(A,S) ... end
When overloading subsasgn, define the method to accept multiple values for the right hand side of the indexed assignment:
function A = subsagn(A,S,varargin)