Forward Indexing Operations - MATLAB & Simulink (original) (raw)
Main Content
The three mixin classes that enable customizing indexing operations—matlab.mixin.indexing.RedefinesParen, matlab.mixin.indexing.RedefinesDot, and matlab.mixin.indexing.RedefinesBrace—work independently. You can implement all three indexing operations, but you also have the option of just implementing one or two of them. You can also choose to customize just one or two levels of indexing and forward additional operations to another MATLAB® object.
For example, this expression shows three levels of indexing:
You can choose to customize only the parentheses indexing by inheriting fromRedefinesParen
and then forward the remaining indexing operations (dot and brace) to the default behaviors. This class fragment inherits fromRedefinesParen
:
classdef MyClass < matlab.mixin.indexing.RedefinesParen
properties (Access = private) prop end
methods (Access = protected) function varargout = parenReference(A,indexOp) idx = indexop(1).Indices;
% Handle customized parentheses indexing
temp = A.prop(idx);
% Forward remaining indexing to temp
[varargout{1:nargout}] = temp.(indexOp(2:end));
end
end end
The forwarding syntax is the dynamic dot indexing syntax with theIndexingOperation
instance:
This expression handles all of the indexing operations after the first parentheses. In other words, the indexing expression described by indexOp(2:end)
is forwarded to temp
. In this example, indexOp(2)
andindexOp(3)
are the dot and brace indexing operations, respectively.temp.(indexOp(2:end))
translates to:
For customized dot indexing, the forwarding syntax maintains the access permissions from the original context of the main indexing expression. For example, after the parentheses indexing obj(1).prop{7}
is handled, MATLAB handles the dot indexing, temp.prop{7}
, using the same context that the initial indexing expression started with. When you callobj(1).prop{7}
inside the class, the private propertyprop
is accessible. When you call obj(1).prop{7}
outside of the class, prop
is not accessible.
See Also
matlab.mixin.indexing.RedefinesParen | matlab.mixin.indexing.RedefinesDot | matlab.mixin.indexing.RedefinesBrace | matlab.indexing.IndexingOperation