Main Content

Object Indexing

Customize subscripted reference and assignment for your class

Customize indexed reference and assignment behavior for object arrays. Although user-defined classes possess the same indexing behavior as built-in MATLAB® classes, you can modify this behavior when your class has special requirements.


numArgumentsFromSubscriptNumber of arguments for customized indexing methods
subsrefSubscripted reference
subsasgnRedefine subscripted assignment
subsindexConvert object to array index
substructCreate structure argument for subsasgn or subsref
builtinExecute built-in function from overloaded method


How Indexing Works

Object Array Indexing

MATLAB classes support object array indexing by default.

Indexed Reference

Learn about subscripted reference.

Indexed Assignment

Learn about subscripted assignment.

Customize Indexing

Code Patterns for subsref and subsasgn Methods

There are code patterns useful for modifying object indexing.

Number of Arguments for subsref and subsasgn

Classes that modify object array indexing must handle the correct number of arguments returned from or passed to indexing methods.

Modify nargout and nargin for Indexing Methods

Modify the number of arguments passed to or returned from indexing methods.

Special Indices

end as Object Index

Learn how to implement the use of end for object indexing.

Objects in Index Expressions

You can design objects that can be used as indices in indexing expressions.

Class with Modified Indexing

This sample class implementation modifies object indexing.