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 case '.' switch index.subs case 'numShares' s.numShares = val; case 'sharePrice' s.sharePrice = val; otherwise s.asset = subsasgn(s.asset,index,val); end end 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 subsasgn method for indices 1 to fieldcount. See “The Asset fieldcount Method” on page 9-49 and “The Asset subsasgn Method” on page 9-47 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 subsasgn method performs field-name error checking. The subsasgn method enables you to assign values to stock object data structure using two techniques. For example, suppose you have a stock object s = stock('XYZ',100,25) You could change the descriptor field with either of the following statements s(1) = 'ABC'; or s.descriptor = 'ABC'; 9-56

Example — Assets and Asset Subclasses See the subsasgn help entry for general information on assignment statements in MATLAB. The Stock display Method When you issue the statement (without terminating with a semicolon) XYZStock = stock('XYZ',100,25) MATLAB looks for a method in the @stock directory called display. The display method for the stock class produces this output. Descriptor: XYZ Date: 17-Nov-1998 Type: stock Current Value: 2500.00 Number of shares: 100 Share price: 25.00 Here is the stock display method. function display(s) % DISPLAY(s) Display a stock object display(s.asset) stg = sprintf('Number of shares: %g\nShare price: %3.2f\n',... s.numShares,s.sharePrice); disp(stg) First, the parent asset object is passed to the asset display method to display its fields (MATLAB calls the asset display method because the input argument is an asset object). The stock object’s fields are displayed in a similar way using a formatted text string. Note that if you did not implement a stock class display method, MATLAB would call the asset display method. This would work, but would display only the descriptor, date, type, and current value. 9-57

9 Classes and Objects<br />

case '.'<br />

switch index.subs<br />

case 'numShares'<br />

s.numShares = val;<br />

case 'sharePrice'<br />

s.sharePrice = val;<br />

otherwise<br />

s.asset = subsasgn(s.asset,index,val);<br />

end<br />

end<br />

The outer switch statement determines if the index is a numeric or field<br />

name syntax.<br />

The fieldcount asset method determines how many fields there are in the<br />

asset structure and the if statement calls the asset subsasgn method for<br />

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

“The Asset subsasgn Method” on page 9-47 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 subsasgn method performs field-name<br />

error checking.<br />

The subsasgn method enables you to assign values to stock object data<br />

structure using two techniques. For example, suppose you have a stock object<br />

s = stock('XYZ',100,25)<br />

You could change the descriptor field with either of the following statements<br />

s(1) = 'ABC';<br />

or<br />

s.descriptor = 'ABC';<br />

9-56

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

Saved successfully!

Ooh no, something went wrong!