Documentation Center

  • Trial Software
  • Product Updates

Containing Assets in a Portfolio

Kinds of Containment

Aggregation is the containment of objects by other objects. The basic relationship is that each contained object "is a part of" the container object. Composition is a more strict form of aggregation in which the contained objects are parts of the containing object and are not associated with any other objects. Portfolio objects form a composition with asset objects because the asset objects are value classes, which are copied when the constructor method creates the DocPortfolio object.

For example, consider a financial portfolio class as a container for a set of assets (stocks, bonds, savings, and so on). It can group, analyze, and return useful information about the individual assets. The contained objects are not accessible directly, but only via the portfolio class methods.

A Simple Class Hierarchy provides information about the assets collected by this portfolio class.

Designing the DocPortfolio Class

The DocPortfolio class is designed to contain the various assets owned by an individual client and to provide information about the status of his or her investment portfolio. This example implements a somewhat over-simplified portfolio class that:

  • Contains an individual's assets

  • Displays information about the portfolio contents

  • Displays a 3-D pie chart showing the relative mix of asset types in the portfolio

Displaying the Class Files

Open the DocPortfolio class definition file in the MATLAB Editor.Open the DocPortfolio class definition file in the MATLAB Editor.

To use the class, create a folder named @DocPortfolio and save DocPortfolio.m to this folder . The parent folder of @DocPortfolio must be on the MATLAB® path.

Summary of the DocPortfolio Class

This class is defined in one file, DocPortfolio.m, which you must place in an @ folder of the same name. The parent folder of the @DocPortfolio folder must on the MATLAB path. See the addpath function for more information.

The following table summarizes the properties defined for the DocPortfolio class.

DocPortfolio Class Properties








Name of client owning the portfolio




A cell array containing individual asset objects




Value of all assets (calculated in the constructor method)

The following table summarizes the methods for the DocPortfolio class.

DocBond Class Methods




Class constructor


Displays information about this object and calls the DocAsset disp method


Overloaded version of pie3 function designed to take a single portfolio object as an argument

Property Definition Block

The following code block shows how the properties are defined:

   Name = '';
properties (SetAccess = private) 
   IndAssets = {};
   TotalValue = 0;

How Class Properties Are Used

  • Name — Stores the name of the client as a character string. The client's name is passed to the constructor as an input argument.

  • IndAsset — A cell array that stores asset objects (i.e., DocStock, DocBond, and DocSavings objects). These asset objects are passed to the DocPortfolio constructor as input arguments and assigned to the property from within the constructor function.

  • IndAsset — The structure of this property is known only to DocPortfolio class member functions so the property's SetAccess attribute is set to private.

  • TotalValue — Stores the total value of the client's assets. The class constructor determines the value of each asset by querying the asset's CurrentValue property and summing the result. Access to the TotalValue property is restricted to DocPortfolio class member functions by setting the property's SetAccess attribute to private.

Using the DocPortfolio Class

The DocPortfolio class is designed to provide information about the financial assets owned by a client. There are three possible types of assets that a client can own: stocks, bonds, and savings accounts.

The first step is to create an asset object to represent each type of asset owned by the client:

XYZStock = DocStock('XYZ Stocks',200,12.34);
USTBonds = DocBond('U.S. Treasury Bonds',1600,3.2,2.8);
SaveAccount = DocSavings('MyBank Acc # 123',2000,6);
VictoriaSelna = DocPortfolio('Victoria Selna',...

The DocPortfolio object displays the following information:

VictoriaSelna = 

Assets for Client: Victoria Selna
Description: XYZ Stocks
Date: 11-Mar-2008
Type: stock
Current Value: $2468.00
Number of shares: 200
Share price: $12.34
Description: MyBank Acc # 123
Date: 11-Mar-2008
Type: savings
Current Value: $2000.00
Interest Rate: 6.00%
Description: U.S. Treasury Bonds
Date: 11-Mar-2008
Type: bond
Current Value: $1828.57
Face value of bonds: $1600
Yield: 3.20%

Total Value: $6296.57

The DocPortfolio pie3 Method provides a graphical display of the portfolio.

The DocPortfolio Constructor Method

The DocPortfolio constructor method takes as input arguments a client's name and a variable length list of asset objects (DocStock, DocBond, and DocSavings objects in this example).

The IndAssets property is a cell array used to store all asset objects. From these objects, the constructor determines the total value of the client's assets. This value is stored in the TotalValue property:

function p = DocPortfolio(name,varargin)
   if nargin > 0
      p.Name = name;
      for k = 1:length(varargin)
         p.IndAssets{k} = varargin(k);
         asset_value = p.IndAssets{k}{1}.CurrentValue;
         p.TotalValue = p.TotalValue + asset_value;
end % DocPortfolio

The DocPortfolio disp Method

The portfolio disp method lists the contents of each contained object by calling the object's disp method. It then lists the client name and total asset value:

function disp(p)
   fprintf('\nAssets for Client: %s\n',p.Name);
   for k = 1:length(p.IndAssets)
      disp(p.IndAssets{k}{1}) % Dispatch to corresponding disp
   fprintf('\nTotal Value: $%0.2f\n',p.TotalValue);
end % disp

The DocPortfolio pie3 Method

The DocPortfolio 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 @DocPortfolio/pie3.m version of pie3 whenever the input argument is a single portfolio object:

function pie3(p)
% Step 1: Get the current value of each asset
   stock_amt = 0; bond_amt = 0; savings_amt = 0;
   for k = 1:length(p.IndAssets)
      if isa(p.IndAssets{k},'DocStock')
         stock_amt = stock_amt + p.IndAssets{k}.CurrentValue;
      elseif isa(p.IndAssets{k},'DocBond')
         bond_amt = bond_amt + p.IndAssets{k}.CurrentValue;
      elseif isa(p.IndAssets{k},'DocSavings')
         savings_amt = savings_amt + p.IndAssets{k}.CurrentValue;
      end % if
   end % for

% Step 2: Create labels and data for the pie graph
   k = 1;
   if stock_amt ~= 0
      label(k) = {'Stocks'};
      pie_vector(k) = stock_amt;
      k = k + 1;
   end % if
   if bond_amt ~= 0
      label(k) = {'Bonds'};
      pie_vector(k) = bond_amt;
      k = k + 1;
   end % if
   if savings_amt ~= 0
      label(k) = {'Savings'};
      pie_vector(k) = savings_amt;
   end % if

% Step 3: Call pie3, adjust fonts and colors
   colormap prism
   stg(1) = {['Portfolio Composition for ',p.Name]};
   stg(2) = {['Total Value of Assets: $',num2str(p.TotalValue,'%0.2f')]};
end % pie3

There are three parts in the overloaded pie3 method.

  • Step 1 — Get the CurrentValue property of each contained asset object and determine the total value in each category.

  • Step 2 — Create the pie chart labels and build a vector of graph data, depending on which objects are present in the portfolio.

  • Step 3 — Call the MATLAB pie3 function, make some font and colormap adjustments, and add a title.

Visualizing a Portfolio

You can use a DocPortfolio object to present an individual's financial portfolio. For example, given the following assets:

XYZStock = DocStock('XYZ Stocks',200,12.34);
USTBonds = DocBond('U.S. Treasury Bonds',1600,3.2,2.8);
SaveAccount = DocSavings('MyBank Acc # 123',2000,6);
VictoriaSelna = DocPortfolio('Victoria Selna',...

you can use the class's pie3 method to display the relative mix of assets as a pie chart.


Was this topic helpful?