This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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.


numArgumentsFromSubscript Number of arguments for customized indexing methods
subsref Redefine subscripted reference for objects
subsasgn Redefine subscripted assignment
subsindex Convert object to array index
substruct Create structure argument for subsasgn or subsref
builtin Execute built-in function from overloaded method


Customize Subscripted Indexing

Object Array Indexing

MATLAB classes support object array indexing by default.

Indexed Reference

Object indexed references are in three forms — parentheses, braces, and dot-name:

Indexed Assignment

Object indexed assignments are in three forms — parentheses, braces, and dot-name:

Code Patterns for subsref and subsasgn Methods

User-defined classes have the same indexing behaviors as that of built-in classes.

Number of Arguments for subsref and subsasgn

MATLAB calls subsref or subsasgn to determine the result of executing code that involves indexed reference or assignment.

Modify nargout and nargin for Indexing Methods

By default, the number of values referenced by an indexing operation determines how many output arguments MATLAB uses to call subsref.

Special Indices

end as Object Index

When you use end in an object indexing expression, such as A(4:end), the end function returns the index value corresponding to the last element in that dimension.

Objects in Index Expressions

MATLAB can use objects as indices in indexed expressions.

Class with Modified Indexing

This example defines a class that modifies the default indexing behavior by implementing subsref and subsasgn methods.

Was this topic helpful?