Programming Previous page   Next Page

The Stock subsref Method

The subsref method defines subscripted indexing for the stock class. In this example, subsref is implemented to enable numeric and structure field name indexing of stock objects.

The outer switch statement determines if the index is a numeric or field name syntax.

The fieldcount asset method determines how many fields there are in the asset structure, and the if statement calls the asset subsref method for indices 1 to fieldcount. See The Asset fieldcount Method and The Asset subsref Method for a description of these methods.

Numeric indices greater than the number returned by fieldcount are handled by the inner switch statement, which maps the index value to the appropriate field in the stock structure.

Field-name indexing assumes field names other than numShares and sharePrice are asset fields, which eliminates the need for knowledge of asset fields by child methods. The asset subsref method performs field-name error checking.

See the subsref help entry for general information on implementing this method.


Previous page  The Stock set Method The Stock subsasgn Method Next page

© 1994-2005 The MathWorks, Inc.