matlab.mixin.indexing.RedefinesBrace Class
Namespace: matlab.mixin.indexing
Description
The matlab.mixin.indexing.RedefinesBrace
class is an abstract superclass
that enables you to customize how indexing operations with braces behave.
RedefinesParen
and RedefinesDot
enable you to customize
indexing operations with parentheses 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 braces, inherit from
RedefinesBrace
and implement its abstract methods:
braceAssign
braceListLength
braceReference
Class Attributes
Abstract | true |
HandleCompatible | true |
For information on class attributes, see Class Attributes.
Methods
Protected Methods
braceAssign | Customize handling of object index assignments that begin with braces |
braceListLength | Determine number of values to return from customized indexing operations beginning with braces |
braceReference | Customize handling of object index references that begin with braces |
Examples
Customize Brace Indexing
This example shows how to customize brace indexing in the ArrayofArrays
class. ArrayOfArrays
implements a 1-by-n
array of arrays. The contained arrays can be of different classes, shapes, and sizes. The class inherits from both matlab.mixin.indexing.RedefinesBrace
and matlab.mixin.indexing.RedefinesParen
. The brace indexing customization handles references and assignments to the contained arrays:
braceReference
: Handles brace references into theArrays
property of the class. For example, the syntaxinstance
{idx
} accesses the contained array at indexidx
inArrays
.braceAssign
: Handles brace assignments into theArrays
property. For example, the syntaxinstance
{idx
} =A
assigns the value ofA
to the contained array at indexidx
inArrays
. Ifidx
exceeds the current number of elements inArrays
,braceAssign
creates the element and assignsA
to it.braceListLength
: Determines the number of values to return from brace indexing expressions that return a comma-separated list.
ArrayOfArrays
also inherits from RedefinesParen
to support parentheses indexing in a way similar to cell arrays. For example, the syntax instance1
(
idx1
)
= instance2
(
idx2
)
assigns the contained array in instance2
at idx2
to idx1
in instance1
. To see the full list of abstract methods that must be implemented when customizing parentheses indexing, see matlab.mixin.indexing.RedefinesParen
.
The class definition of ArrayOfArrays
also includes two error-handling functions:
ErrorForUnsupportedIndexing:
Prevents any additional indexing after parentheses indexing in reference and assignment operations.ErrorForUnexpectedRightHandSide:
Restricts parentheses assignments to cases when the right-hand side of the statement is anArrayofArrays
instance.
Code for ArrayOfArrays
Class and Error-Handling Functions
classdef ArrayOfArrays < matlab.mixin.indexing.RedefinesParen & ... matlab.mixin.indexing.RedefinesBrace properties (Access=private) Arrays (1,:) cell end methods (Access=protected) function varargout = braceReference(obj,indexOp) [varargout{1:nargout}] = obj.Arrays.(indexOp); end function obj = braceAssign(obj,indexOp,varargin) if isscalar(indexOp) [obj.Arrays.(indexOp)] = varargin{:}; return; end [obj.Arrays.(indexOp)] = varargin{:}; end function n = braceListLength(obj,indexOp,indexContext) n = listLength(obj.Arrays,indexOp,indexContext); end function out = parenReference(obj,indexOp) ErrorForUnsupportedIndexing(indexOp); out = obj; out.Arrays = obj.Arrays.(indexOp); end function obj = parenAssign(obj,indexOp,in) ErrorForUnsupportedIndexing(indexOp); ErrorForUnexpectedRightHandSide(in); obj.Arrays.(indexOp) = in.Arrays; end function n = parenListLength(~,~,~) n = 1; end function obj = parenDelete(obj,indexOp) obj.Arrays.(indexOp) = []; end end methods (Access=public) function obj = ArrayOfArrays(varargin) if nargin > 0 obj.Arrays = varargin; else obj.Arrays = []; end end function out = cat(~,varargin) out = ArrayOfArrays; for ix = 1:length(varargin) tmp = varargin{ix}; if isa(tmp,'ArrayOfArrays') out.Arrays = [out.Arrays,tmp.Arrays]; else out.Arrays{end+1} = tmp; end end end function varargout = size(obj,varargin) [varargout{1:nargout}] = size(obj.Arrays,varargin{:}); end end methods (Static) function out = empty() out = ArrayOfArrays(); end end end function ErrorForUnsupportedIndexing(indexOp) if ~isscalar(indexOp) error('Indexing after parentheses indexing is not supported.'); end end function ErrorForUnexpectedRightHandSide(val) if ~isa(val,'ArrayOfArrays') error(['Parentheses assignment is only supported when the ' ... 'right-hand side is an ArrayOfArrays.']); end end
Use an ArrayOfArrays
Instance
Create an ArrayOfArrays
instance with two contained arrays, a 2-element vector and a 3-by-3 matrix.
myArrays = ArrayOfArrays([1 2],2*eye(3));
Use brace indexing to view the second contained array. The braceReference
method accepts an IndexingOperation
object that identifies what field to access.
myArrays{2}
ans = 3×3
2 0 0
0 2 0
0 0 2
Use brace and parentheses indexing to extract the upper-right corner of the matrix in the second array. The class calls the braceReference
method to access the contained array at index 2 and then forwards the parentheses indexing to the matrix itself.
myArrays{2}(1:2,2:3)
ans = 2×2
0 0
2 0
Add a third element to the instance using brace assignment. The braceAssign
method accepts an IndexingOperation
object, which describes the type of indexing operation (Brace
) and the indices referenced, and a second argument that contains the value to be assigned, in this case a cell array. Verify the addition.
myArrays{3} = {1:5,6:10}; myArrays{3}
ans=1×2 cell array
{[1 2 3 4 5]} {[6 7 8 9 10]}
Assign multiple new values to myArrays{3}
. Because the assignment involves multiple values, MATLAB® calls the braceListLength
method to determine how many elements need to be assigned before calling braceAssign
to handle the assignment operation.
[myArrays{3}{:}] = deal(5); myArrays{3}
ans=1×2 cell array
{[5]} {[5]}
The customized parentheses indexing implemented by the class enables parentheses references and assignment in a similar way to how this indexing works in cell arrays. Assign the contained arrays at indices 1 and 2 of myArrays
to a new ArrayOfArrays
instance, x
. The class calls parenReference
to perform the operation. Use brace indexing to verify the contents of x
.
x = myArrays(1:2); x{1:2}
ans = 1×2
1 2
ans = 3×3
2 0 0
0 2 0
0 0 2
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 (한국어)