MATLAB Object-Oriented Programming

(Joyce) #1

Code Patterns for subsref and subsasgn Methods


In this section...
“Customize Indexed Reference and Assignment” on page 17-21
“Syntax for subsref and subsasgn Methods” on page 17-21
“Indexing Structure Describes Indexing Expressions” on page 17-22
“Values of the Indexing Structure” on page 17-23
“Typical Patterns for Indexing Methods” on page 17-23

Customize Indexed Reference and Assignment


User-defined classes have the same indexing behaviors as that of built-in classes. Classes
can customize indexing operations by overloading the functions that MATLAB calls to
evaluate indexing expressions. Overload the subsref and subsasgn functions when you
want to define special behaviors for indexed reference and assignment.

For an overview of object indexing, see “Object Array Indexing” on page 17-15.

Syntax for subsref and subsasgn Methods


MATLAB calls the subsref and subsasgn methods of your class with these arguments.

Method Input Output
b = subsref(obj,s) • obj — Object or object
array used in indexing
expression


  • s — Indexing structure


b — Result of indexing
expression

obj =
subsasgn(obj,s,b)


  • obj — Object or object
    array used in indexing
    expression

  • s — Indexing structure

  • b — Value being assigned


obj — Object or object
array after assignment

Modifying Number of Arguments

If your class design requires that indexing operations return or assign a different number
of values than the number defined by the default indexing operation, overload the

Code Patterns for subsref and subsasgn Methods
Free download pdf