Example -- Defining saveobj and loadobj for Portfolio
In the section Example -- The Portfolio Container, 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,
accountNumber, 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
|Saving and Loading Objects||The saveobj Method|
© 1994-2005 The MathWorks, Inc.