23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Designing User Classes in <strong>MATLAB</strong><br />

Indexed Reference Using subsref and subsasgn<br />

Userclassesimplementnewdatatypesin<strong>MATLAB</strong>.Itisusefultobeableto<br />

access object data via an indexed reference, as is possible with the <strong>MATLAB</strong><br />

built-in data types. For example, if A is an array of class double, A(i) returns<br />

the i th element of A.<br />

As the class designer, you can decide what an index reference to an object<br />

means. For example, suppose you define a class that creates polynomial<br />

objects and these objects contain the coefficients of the polynomial.<br />

An indexed reference to a polynomial object,<br />

p(3)<br />

could return the value of the coefficient of x 3 , the value of the polynomial at x<br />

= 3, or something different depending on the intended design.<br />

You define the behavior of indexing for a particular class by creating two class<br />

methods - subsref and subsasgn. <strong>MATLAB</strong> calls these methods whenever a<br />

subscripted reference or assignment is made on an object from the class. If<br />

you do not define these methods for a class, indexing is undefined for objects<br />

of this class.<br />

In general, the rules for indexing objects are the same as the rules for indexing<br />

structure arrays. For details, see “Structures” on page 2-75.<br />

Behavior Within Class Methods<br />

If A is an array of one of the fundamental <strong>MATLAB</strong> data types, then<br />

referencing a value of A using an indexed reference calls the built-in <strong>MATLAB</strong><br />

subsref method. It does not call any subsref method that you might have<br />

overloaded for that data type.<br />

For example, if A is an array of type double, and there is an @double/subsref<br />

method on your <strong>MATLAB</strong> path, the statement B = A(I) does not call this<br />

method, but calls the <strong>MATLAB</strong> built-in subsref method instead.<br />

The same is true for user-defined classes. Whenever a class method requires<br />

the functionality of the overloaded subsref or subsassign, it must call the<br />

9-15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!