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.

9 Classes and Objects<br />

• subsref — A new portfolio method that enables subscripted reference to<br />

portfolio objects outside of a portfolio method.<br />

• getAccountNumber — a <strong>MATLAB</strong> function that returns an account number<br />

that consists of the first three letters of the client’s name.<br />

New Portfolio Class Behavior<br />

With the additions and changes made in this example, the portfolio class now<br />

• Includes a field for an account number<br />

• Adds the account number when a portfolio object is saved for the first time<br />

• Automatically updates the older version of portfolio objects when you load<br />

them into the <strong>MATLAB</strong> workspace<br />

The saveobj Method<br />

<strong>MATLAB</strong> looks for the portfolio saveobj method whenever the save command<br />

is passed a portfolio object. If @portfolio/saveobj exists, <strong>MATLAB</strong> passes<br />

the portfolio object to saveobj, which must then return the modified object as<br />

an output argument. The following implementation of saveobj determines if<br />

the object has already been assigned an account number from a previous save<br />

operation. If not, saveobj calls getAccountNumber to obtain the number and<br />

assigns it to the accountNumber field.<br />

function b = saveobj(a)<br />

if isempty(a.accountNumber)<br />

a.accountNumber = getAccountNumber(a);<br />

end<br />

b = a;<br />

The loadobj Method<br />

<strong>MATLAB</strong> looks for the portfolio loadobj method whenever the load command<br />

detects portfolio objects in the .mat file being loaded. If loadobj exists,<br />

<strong>MATLAB</strong> passes the portfolio object to loadobj, which must then return<br />

the modified object as an output argument. The output argument is then<br />

loaded into the workspace.<br />

If the input object does not match the current definition as specified by the<br />

constructor function, then <strong>MATLAB</strong> converts it to a structure containing the<br />

9-66

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

Saved successfully!

Ooh no, something went wrong!