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.

9 Classes and Objects<br />

end<br />

if savingsAmt ~= 0<br />

label(i) = {'Savings'};<br />

pieVector(i) = savingsAmt;<br />

end<br />

pie3(pieVector, label)<br />

set(gcf, 'Renderer', 'zbuffer')<br />

set(findobj(gca, 'Type', 'Text'), 'FontSize', 14)<br />

cm = gray(64);<br />

colormap(cm(48:end, :))<br />

stg(1) = {['Portfolio Composition for ', p.name]};<br />

stg(2) = {['Total Value of Assets: $', num2str(p.totalValue)]};<br />

title(stg, 'FontSize', 12)<br />

There are three parts in the overloaded pie3 method.<br />

• The first uses the asset subclass get methods to access the CurrentValue<br />

property of each contained object. The totalvalueofeachclassissummed.<br />

• The second part creates the pie chart labels and builds a vector of graph<br />

data, depending on which objects are present.<br />

• The third part calls the <strong>MATLAB</strong> pie3 function, makes some font and<br />

colormap adjustments, and adds a title.<br />

Creating a Portfolio<br />

Suppose you have implemented a collection of asset subclasses in a manner<br />

similar to the stock class. You can then use a portfolio object to present the<br />

individual’s financial portfolio. For example, given the following assets<br />

XYZStock = stock('XYZ', 200, 12);<br />

SaveAccount = savings('Acc # 1234', 2000, 3.2);<br />

Bonds = bond('U.S. Treasury', 1600, 12);<br />

create a portfolio object:<br />

p = portfolio('Gilbert Bates',XYZStock,SaveAccount,Bonds)<br />

The portfolio display method summarizes the portfolio contents (because this<br />

statement is not terminated by a semicolon).<br />

9-62

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

Saved successfully!

Ooh no, something went wrong!