MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

9 Classes and Objects 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. function b = subsref(s,index) % SUBSREF Define field name indexing for stock objects fc = fieldcount(s.asset); switch index.type case '()' if (index.subs{:}

Example — Assets and Asset Subclasses indices 1 to fieldcount. See “The Asset fieldcount Method” on page 9-49 and “The Asset subsref Method” on page 9-46 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. The Stock subsasgn Method The subsasgn method enables you to change the data contained in a stock object using numeric indexing and structure field name indexing. MATLAB calls subsasgn whenever you execute an assignment statement (e.g., A(i) = val, A{i} = val, orA.fieldname = val). function s = subsasgn(s,index,val) % SUBSASGN Define index assignment for stock objects fc = fieldcount(s.asset); switch index.type case '()' if (index.subs{:}

Example — Assets and Asset Subclasses<br />

indices 1 to fieldcount. See “The Asset fieldcount Method” on page 9-49 and<br />

“The Asset subsref Method” on page 9-46 for a description of these methods.<br />

Numeric indices greater than the number returned by fieldcount are<br />

handled by the inner switch statement, which maps the index value to the<br />

appropriate field in the stock structure.<br />

Field name indexing assumes field names other than numShares and<br />

sharePrice are asset fields, which eliminates the need for knowledge of asset<br />

fields by child methods. The asset subsref method performs field-name error<br />

checking.<br />

See the subsref help entry for general information on implementing this<br />

method.<br />

The Stock subsasgn Method<br />

The subsasgn method enables you to change the data contained in a stock<br />

object using numeric indexing and structure field name indexing. <strong>MATLAB</strong><br />

calls subsasgn whenever you execute an assignment statement (e.g., A(i) =<br />

val, A{i} = val, orA.fieldname = val).<br />

function s = subsasgn(s,index,val)<br />

% SUBSASGN Define index assignment for stock objects<br />

fc = fieldcount(s.asset);<br />

switch index.type<br />

case '()'<br />

if (index.subs{:}

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

Saved successfully!

Ooh no, something went wrong!