Programming Previous page   Next Page

Indexed Reference Using subsref and subsasgn

User classes implement new data types in MATLAB. It is useful to be able to access object data via an indexed reference, as is possible with the MATLAB built-in data types. For example, if A is an array of class double, A(i) returns the ith element of A.

As the class designer, you can decide what an index reference to an object means. For example, suppose you define a class that creates polynomial objects and these objects contain the coefficients of the polynomial.

An indexed reference to a polynomial object,

could return the value of the coefficient of x3, the value of the polynomial at
x = 3, or something different depending on the intended design.

You define the behavior of indexing for a particular class by creating two class methods - subsref and subsasgn. MATLAB calls these methods whenever a subscripted reference or assignment is made on an object from the class. If you do not define these methods for a class, indexing is undefined for objects of this class.

In general, the rules for indexing objects are the same as the rules for indexing structure arrays. For details, see Structures.


Previous page  The set and get Methods Handling Subscripted Reference Next page

© 1994-2005 The MathWorks, Inc.