23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example — Assets and Asset Subclasses<br />

TheStocksetMethod<br />

The set method provides a “property name” interface like the get method.<br />

It is designed to update the number of shares, the share value, and the<br />

descriptor. The current value and the date are automatically updated.<br />

function s = set(s,varargin)<br />

% SET Set stock properties to the specified values<br />

% and return the updated object<br />

propertyArgIn = varargin;<br />

while length(propertyArgIn) >= 2,<br />

prop = propertyArgIn{1};<br />

val = propertyArgIn{2};<br />

propertyArgIn = propertyArgIn(3:end);<br />

switch prop<br />

case 'NumberShares'<br />

s.numShares = val;<br />

case 'SharePrice'<br />

s.sharePrice = val;<br />

case 'Descriptor'<br />

s.asset = set(s.asset,'Descriptor',val);<br />

otherwise<br />

error('Invalid property')<br />

end<br />

end<br />

s.asset = set(s.asset,'CurrentValue',...<br />

s.numShares * s.sharePrice,'Date',date);<br />

Note that this function creates and returns a new stock object with the new<br />

values, which you then copy over the old value. For example, given the stock<br />

object,<br />

s = stock('XYZ',100,25);<br />

the following set command updates the share price.<br />

s = set(s,'SharePrice',36);<br />

It is necessary to copy over the original stock object (i.e., assign the output to<br />

s) because <strong>MATLAB</strong> does not support passing arguments by reference. Hence<br />

the set method actually operates on a copy of the object.<br />

9-53

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

Saved successfully!

Ooh no, something went wrong!