23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example — Defining saveobj and loadobj for Portfolio<br />

Example — Defining saveobj and loadobj for Portfolio<br />

This example is divided into the following topics:<br />

• “Summary of Code Changes” on page 9-65<br />

• “The saveobj Method” on page 9-66<br />

• “The loadobj Method” on page 9-66<br />

• “Changing the Portfolio Constructor” on page 9-67<br />

• “The Portfolio subsref Method” on page 9-68<br />

In the section “Example — The Portfolio Container” on page 9-58, portfolio<br />

objects are used to collect information about a client’s investment portfolio.<br />

Now suppose you decide to add an account number to each portfolio object<br />

that is saved. You can define a portfolio saveobj method to carry out this task<br />

automatically during the save operation.<br />

Suppose further that you have already saved a number of portfolio objects<br />

without the account number. You want to update these objects during the<br />

load operation so that they are still valid portfolio objects. You can do this by<br />

defining a loadobj method for the portfolio class.<br />

Summary of Code Changes<br />

To implement the account number scenario, you need to add or change the<br />

following functions:<br />

• portfolio — The portfolio constructor method needs to be modified to<br />

create a new field, accountNumber, which is initialized to the empty string<br />

when an object is created.<br />

• saveobj — A new portfolio method designed to add an account number<br />

to a portfolio object during the save operation, only if the object does not<br />

already have one.<br />

• loadobj — A new portfolio method designed to update older versions of<br />

portfolio objects that were saved before the account number structure field<br />

was added.<br />

9-65

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

Saved successfully!

Ooh no, something went wrong!