MATLAB Object-Oriented Programming

(Joyce) #1

Class with Modified Indexing


In this section...
“How to Modify Class Indexing” on page 17-39
“Class Description” on page 17-39
“Specialize Subscripted Reference — subsref” on page 17-40
“Specialize Subscripted Assignment — subsasgn” on page 17-42
“Implement Addition for Object Data — double and plus” on page 17-43
“MyDataClass.m” on page 17-44

How to Modify Class Indexing


This example defines a class that modifies the default indexing behavior by implementing
subsref and subsasgn methods. The class also implements type conversion and
addition by implementing a double converter method and a plus method.

The objective of the class design is to:


  • Enable you to treat an object of the class as a numeric array

  • Be able to contain nonnumeric and numeric data in an object of the class


Class Description


The class has three properties:


  • Data — numeric test data

  • Description — description of test data

  • Date — date test was conducted


Assume that you have the following random data (randi):

d = randi(9,3,4)

d =

8 9 3 9
9 6 5 2
2 1 9 9

Class with Modified Indexing
Free download pdf