name]};
stg(2) = {['Total Value of Assets: $',num2str(p.total_value)]};
title(stg,'FontSize',12)
There are three parts in the overloaded pie3 method.
• The first uses the asset subclass get methods to access the CurrentValue
property of each contained object. The total value of each class is summed.
• The second part creates the pie chart labels and builds a vector of graph data,
depending on which objects are present.
• The third part calls the MATLAB pie3 function, makes some font and
colormap adjustments, and adds a title.
Creating a Portfolio
Suppose you have implemented a collection of asset subclasses in a manner
similar to the stock class. You can then use a portfolio object to present the
individual’s financial portfolio. For example, given the following assets
XYZStock = stock('XYZ',200,12);
SaveAccount = savings('Acc # 1234',2000,3.2);
Bonds = bond('U.S. Treasury',1600,12);
Create a portfolio object.
p = portfolio('Gilbert Bates',XYZStock,SaveAccount,Bonds)
The portfolio display method summarizes the portfolio contents (because this
statement is not terminated by a semicolon).
Descriptor: XYZ
Date: 24-Nov-1998
Current Value: 2400.00
Type: stock
Number of shares: 200
Share price: 12.00
Descriptor: Acc # 1234
Date: 24-Nov-1998
Current Value: 2000.00
Type: savings
Interest Rate: 3.2%
22-58
Example - The Portfolio Container
Descriptor: U.S. Treasury
Date: 24-Nov-1998
Current Value: 1600.00
Type: bond
Interest Rate: 12%
Assets for Client: Gilbert Bates
Total Value: 6000.00
The portfolio pie3 method displays the relative mix of assets using a pie chart.
pie3(p)
22-59
22 MATLAB Classes and Objects
Saving and Loading Objects
You can use the MATLAB save and load commands to save and retrieve
user-defined objects to and from .mat files, just like any other variables.
When you load objects, MATLAB calls the object’s class constructor to register
the object in the workspace. The constructor function for the object class you
are loading must be able to be called with no input arguments and return a
default object. See “Guidelines for Writing a Constructor” on page 22-10 for
more information.
Modifying Objects During Save or Load
When you issue a save or load command on objects, MATLAB looks for class
methods called saveobj and loadobj in the class directory. You can overload
these methods to modify the object before the save or load operation. For
example, you could define a saveobj method that saves related data along with
the object or you could write a loadobj method that updates objects to a newer
version when this type of object is loaded into the MATLAB workspace.
22-60
Example - Defining saveobj and loadobj for Portfolio
Example - Defining saveobj and loadobj for Portfolio
In the section “Example - The Portfolio Container” on page 22-54, portfolio
objects are used to collect information about a client’s investment portfolio.
Now suppose you decide to add an account number to each portfolio object that
is saved. You can define a portfolio saveobj method to carry out this task
automatically during the save operation.
Suppose further that you have already saved a number of portfolio objects
without the account number. You want to update these objects during the load
operation so that they are still valid portfolio objects. You can do this by
defining a loadobj method for the portfolio class.
Summary of Code Changes
To implement the account number scenario, you need to add or change the
following functions:
• portfolio – The portfolio constructor method needs to be modified to create
a new field, account_number, which is initialized to the empty string when
an object is created.
• saveobj – A new portfolio method designed to add an account number to a
portfolio object during the save operation, only if the object does not already
have one.
• loadobj – A new portfolio method designed to update older versions of
portfolio objects that were saved before the account number structure field
was added.
• subsref – A new portfolio method that enables subscripted reference to
portfolio objects outside of a portfolio method.
• getAccountNumber – a MATLAB function that returns an account number
that consists of the first three letters of the client’s name.
New Portfolio Class Behavior
With the additions and changes made in this example, the portfolio class now:
• Includes a field for an account number
• Adds the account number when a portfolio object is saved for the first time
22-61
22 MATLAB Classes and Objects
• Automatically updates the older version of portfolio objects when you load
them into the MATLAB workspace
The saveobj Method
MATLAB looks for the portfolio saveobj method whenever the save command
is passed a portfolio object. If @portfolio/saveobj exists, MATLAB passes the
portfolio object to saveobj, which must then return the modified object as an
output argument. The following implementation of saveobj determines if the
object has already been assigned an account number from a previous save
operation. If not, saveobj calls getAccountNumber to obtain the number and
assigns it to the account_number field.
function b = saveobj(a)
if isempty(a.account_number)
a.account_number = getAccountNumber(a);
end
b = a;
The loadobj Method
MATLAB looks for the portfolio loadobj method whenever the load command
detects portfolio objects in the .mat file being loaded. If loadobj exists,
MATLAB passes the portfolio object to loadobj, which must then return the
modified object as an output argument. The output argument is then loaded
into the workspace.
If the input object does not match the current definition as specified by the
constructor function, then MATLAB converts it to a structure containing the
same fields and the object’s structure with all the values intact (that is, you
now have a structure, not an object).