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 />

For example, this statement creates a stock object to record the ownership of<br />

100 shares of XYZ corporation stocks with a price per share of 25 dollars.<br />

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

The Stock get Method<br />

The get method provides a way to access the data in the stock object using a<br />

“property name” style interface, similar to Handle Graphics. While in this<br />

example the property names are similar to the structure field name, they can<br />

be quite different. You could also choose to exclude certain fields from access<br />

via the get method or return the data from the same field for a variety of<br />

property names, if such behavior suits your design.<br />

function val = get(s,propName)<br />

% GET Get stock property from the specified object<br />

% and return the value. Property names are: NumberShares<br />

% SharePrice, Descriptor, Date, CurrentValue<br />

switch propName<br />

case 'NumberShares'<br />

val = s.numShares;<br />

case 'SharePrice'<br />

val = s.sharePrice;<br />

case 'Descriptor'<br />

val = get(s.asset,'Descriptor'); % call asset get method<br />

case 'Date'<br />

val = get(s.asset,'Date');<br />

case 'CurrentValue'<br />

val = get(s.asset,'CurrentValue');<br />

otherwise<br />

error([propName ,'Is not a valid stock property'])<br />

end<br />

Note that the asset object is accessed via the stock object’s asset field<br />

(s.asset). <strong>MATLAB</strong> automatically creates this field when the class function<br />

is called with the parent argument.<br />

9-52

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

Saved successfully!

Ooh no, something went wrong!