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.

Example — The Portfolio Container<br />

function display(p)<br />

% DISPLAY Display a portfolio object<br />

for k = 1:length(p.indAssets)<br />

display(p.indAssets{k})<br />

end<br />

stg = sprintf('\nAssets for Client: %s\nTotal Value: %9.2f\n',...<br />

p.name,p.totalValue);<br />

disp(stg)<br />

The Portfolio pie3 Method<br />

The portfolio class overloads the <strong>MATLAB</strong> pie3 function to accept a portfolio<br />

object and display a 3-D pie chart illustrating the relative asset mix of the<br />

client’s portfolio. <strong>MATLAB</strong> calls the @portfolio/pie3.m version of pie3<br />

whenever the input argument is a single portfolio object.<br />

function pie3(p)<br />

% PIE3 Create a 3-D pie chart of a portfolio<br />

stockAmt = 0; bondAmt = 0; savingsAmt = 0;<br />

for k = 1:length(p.indAssets)<br />

if isa(p.indAssets{k}, 'stock')<br />

stockAmt = stockAmt + ...<br />

get(p.indAssets{k}, 'CurrentValue');<br />

elseif isa(p.indAssets{k}, 'bond')<br />

bondAmt = bondAmt + ...<br />

get(p.indAssets{k}, 'CurrentValue');<br />

elseif isa(p.indAssets{k}, 'savings')<br />

savingsAmt = savingsAmt + ...<br />

get(p.indAssets{k}, 'CurrentValue');<br />

end<br />

end<br />

i = 1;<br />

if stockAmt ~= 0<br />

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

pieVector(i) = stockAmt;<br />

i = i + 1;<br />

end<br />

if bondAmt ~= 0<br />

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

pieVector(i) = bondAmt;<br />

i = i + 1;<br />

9-61

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

Saved successfully!

Ooh no, something went wrong!