Programming |
The portfolio class overloads the MATLAB pie3
function to accept a portfolio object and display a 3-D pie chart illustrating the relative asset mix of the client's portfolio. MATLAB calls the @portfolio/pie3.m
version of pie3
whenever the input argument is a single portfolio object.
function pie3(p) % PIE3 Create a 3-D pie chart of a portfolio stockAmt = 0; bondAmt = 0; savingsAmt = 0; for k = 1:length(p.indAssets) if isa(p.indAssets{k}, 'stock') stockAmt = stockAmt + ... get(p.indAssets{k}, 'CurrentValue'); elseif isa(p.indAssets{k}, 'bond') bondAmt = bondAmt + ... get(p.indAssets{k}, 'CurrentValue'); elseif isa(p.indAssets{k}, 'savings') savingsAmt = savingsAmt + ... get(p.indAssets{k}, 'CurrentValue'); end end i = 1; if stockAmt ~= 0 label(i) = {'Stocks'}; pieVector(i) = stockAmt; i = i + 1; end if bondAmt ~= 0 label(i) = {'Bonds'}; pieVector(i) = bondAmt; i = i + 1; end if savingsAmt ~= 0 label(i) = {'Savings'}; pieVector(i) = savingsAmt; end pie3(pieVector, label) set(gcf, 'Renderer', 'zbuffer') set(findobj(gca, 'Type', 'Text'), 'FontSize', 14) cm = gray(64); colormap(cm(48:end, :)) stg(1) = {['Portfolio Composition for ', p.name]}; stg(2) = {['Total Value of Assets: $', num2str(p.totalValue)]}; title(stg, 'FontSize', 12)
There are three parts in the overloaded pie3
method.
get
methods to access the CurrentValue
property of each contained object. The total value of each class is summed.
pie3
function, makes some font and colormap adjustments, and adds a title.
The Portfolio display Method | Creating a Portfolio |
© 1994-2005 The MathWorks, Inc.