23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example — Assets and Asset Subclasses<br />

See the subsasgn help entry for general information on assignment<br />

statements in <strong>MATLAB</strong>.<br />

The Stock display Method<br />

When you issue the statement (without terminating with a semicolon)<br />

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

<strong>MATLAB</strong> looks for a method in the @stock directory called display. The<br />

display method for the stock class produces this output.<br />

Descriptor: XYZ<br />

Date: 17-Nov-1998<br />

Type: stock<br />

Current Value: 2500.00<br />

Number of shares: 100<br />

Share price: 25.00<br />

Here is the stock display method.<br />

function display(s)<br />

% DISPLAY(s) Display a stock object<br />

display(s.asset)<br />

stg = sprintf('Number of shares: %g\nShare price: %3.2f\n',...<br />

s.numShares,s.sharePrice);<br />

disp(stg)<br />

First, the parent asset object is passed to the asset display method to<br />

display its fields (<strong>MATLAB</strong> calls the asset display method because the input<br />

argument is an asset object). The stock object’s fields are displayed in a<br />

similar way using a formatted text string.<br />

Note that if you did not implement a stock class display method, <strong>MATLAB</strong><br />

would call the asset display method. This would work, but would display<br />

only the descriptor, date, type, and current value.<br />

9-57

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

Saved successfully!

Ooh no, something went wrong!