matlab.mixin.indexing.RedefinesParen Class
Namespace: matlab.mixin.indexing
Description
The matlab.mixin.indexing.RedefinesParen
class is an abstract superclass
that enables you to customize how indexing operations with parentheses behave.
RedefinesBrace
and RedefinesDot
enable you to customize
indexing operations with curly braces and dots. You can inherit from these classes
individually, customizing one aspect of behavior without affecting the default behavior of the
other indexing operations.
To customize how your class handles indexing operations with parentheses, inherit from
RedefinesParen
and implement its abstract methods:
cat
empty
size
parenAssign
parenDelete
parenListLength
parenReference
Class Attributes
Abstract | true |
HandleCompatible | true |
For information on class attributes, see Class Attributes.
Methods
Specialized Operators and Functions
These methods specialize standard MATLAB® operators and functions for objects in this class.
cat | Implement this method with signature
| ||||
empty | Implement this method with signature
| ||||
size | Implement this method with signature
| ||||
ctranspose |
| ||||
end |
| ||||
horzcat |
| ||||
isempty |
| ||||
length |
| ||||
ndims |
| ||||
numel |
| ||||
reshape |
| ||||
transpose |
| ||||
vertcat |
|
Protected Methods
parenAssign | Customize handling of object index assignments that begin with parentheses |
parenDelete | Customize handling of object index deletions |
parenListLength | Determine number of values to return from customized indexing operations beginning with parentheses |
parenReference | Customize handling of object index references that begin with parentheses |
Examples
Customize Parentheses Indexing
The ArrayWithLabel
class has two properties: ContainedArray
and Label
. ArrayWithLabel
customizes parentheses indexing into ContainedArray
by inheriting from matlab.mixin.indexing.RedefinesParen
and implementing all of its abstract methods:
parenReference
: Handles parentheses indexing intoContainedArray
.parenDelete
: Deletes parentheses-indexed elements ofContainedArray
.parenAssign
: Assigns values to the indexed elements ofContainedArray
. The right-hand side of the assignment expression must be an instance ofArrayWithLabel
.parenListLength
: Determines the number of values to return from parentheses indexing operations onContainedArray
.cat
: Concatenates theContainedArray
property of one or more instances of the class.empty
: Returns an instance of the class with an emptyContainedArray
.size
: Returns the dimensions ofContainedArray
.
ArrayWithLabel
also provides two public methods:
value
: Displays the indexed values ofContainedArray
.sum
: Calculates the sum of the indexed values ofContainedArray
.
ArrayWithLabel
Class Code
classdef ArrayWithLabel < matlab.mixin.indexing.RedefinesParen properties (Access=private) ContainedArray end properties (Access=public) Label end methods function obj = ArrayWithLabel(val) obj.ContainedArray = val; end end methods (Access=protected) function varargout = parenReference(obj, indexOp) obj.ContainedArray = obj.ContainedArray.(indexOp(1)); if isscalar(indexOp) varargout{1} = obj; return; end [varargout{1:nargout}] = obj.(indexOp(2:end)); end function obj = parenAssign(obj,indexOp,varargin) % Ensure object instance is the first argument of call. if isempty(obj) obj = varargin{1}; end if isscalar(indexOp) assert(nargin==3); rhs = varargin{1}; obj.ContainedArray.(indexOp) = rhs.ContainedArray; return; end [obj.(indexOp(2:end))] = varargin{:}; end function n = parenListLength(obj,indexOp,ctx) if numel(indexOp) <= 2 n = 1; return; end containedObj = obj.(indexOp(1:2)); n = listLength(containedObj,indexOp(3:end),ctx); end function obj = parenDelete(obj,indexOp) obj.ContainedArray.(indexOp) = []; end end methods (Access=public) function out = value(obj) out = obj.ContainedArray; end function out = sum(obj) out = sum(obj.ContainedArray,"all"); end function out = cat(dim,varargin) numCatArrays = nargin-1; newArgs = cell(numCatArrays,1); for ix = 1:numCatArrays if isa(varargin{ix},'ArrayWithLabel') newArgs{ix} = varargin{ix}.ContainedArray; else newArgs{ix} = varargin{ix}; end end out = ArrayWithLabel(cat(dim,newArgs{:})); end function varargout = size(obj,varargin) [varargout{1:nargout}] = size(obj.ContainedArray,varargin{:}); end end methods (Static, Access=public) function obj = empty() obj = ArrayWithLabel([]); end end end
Use an ArrayWithLabel
Instance
Construct an ArrayWithLabel
object with a 2-by-2 matrix, and assign a string to the Label
property.
a = ArrayWithLabel([2 3; 5 7]);
a.Label = "primes"
a=2×2 ArrayWithLabel array with properties:
Label: "primes"
Display the first column of the array. parenReference
takes a
and an instance of IndexingOperation
as arguments. indexOp
identifies the type of reference (Paren
) and the indices being referenced. parenReference
retrieves the elements corresponding to those indices and then forwards the value
method call to MATLAB. (The comment in the code identifies the line in parenReference
that forwards additional operations after the initial parentheses indexing.)
a(:,1).value
ans = 2×1
2
5
Create a new instance b
of ArrayWithLabel
with a 1-by-2 vector. Assign the values of b
to the second row of the array in a
. The parenAssign
method uses the indices on the left-hand side of the assignment to determine which elements of a
to replace.
b = ArrayWithLabel([11 13]); a(2,:) = b; a.value
ans = 2×2
2 3
11 13
Use the sum
method to find the sum of the values in the second column.
a(:,2).sum
ans = 16
Version History
Introduced in R2021b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)