Main Content

Forward Indexing Operations

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:

obj(1).prop{7}

You can choose to customize only the parentheses indexing by inheriting from RedefinesParen and then forward the remaining indexing operations (dot and brace) to the default behaviors. This class fragment inherits from RedefinesParen:

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 the IndexingOperation instance:

temp.(indexOp(2:end))

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) and indexOp(3) are the dot and brace indexing operations, respectively. temp.(indexOp(2:end)) translates to:

temp.prop{7}

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 call obj(1).prop{7} inside the class, the private property prop is accessible. When you call obj(1).prop{7} outside of the class, prop is not accessible.

See Also

| | |

Related Topics