MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

9 Classes and Objects Examples of Constructor Methods See the following sections for examples of constructor methods: • “The Polynom Constructor Method” on page 9-27 • “The Asset Constructor Method” on page 9-43 • “The Stock Constructor Method” on page 9-50 • “The Portfolio Constructor Method” on page 9-59 Identifying Objects Outside the Class Directory The class and isa functions used in constructor methods can also be used outside of the class directory. The expression isa(a,'classname'); checks whether a is an object of the specified class. For example, if p is a polynom object, each of the following expressions is true. isa(pi,'double'); isa('hello','char'); isa(p,'polynom'); Outside of the class directory, the class function takes only one argument (it is only within the constructor that class canhavemorethanoneargument). The expression class(a) returns a string containing the class name of a. For example, class(pi), class('hello'), class(p) return 'double', 'char', 'polynom' 9-12

Designing User Classes in MATLAB Use the whos function to see what objects are in the MATLAB workspace. whos Name Size Bytes Class p 1x1 156 polynom object The display Method MATLAB calls a method named display whenever an object is the result of a statement that is not terminated by a semicolon. For example, creating the variable a, which is a double, calls the MATLAB display method for doubles. a = 5 a = 5 You should define a display method so MATLAB can display values on the command line when referencing objects from your class. In many classes, display can simply print the variable name, and then use the char converter method to print the contents or value of the variable, since MATLAB displays output as strings. You must define the char method to convert the object’s data to a character string. Examples of display Methods See the following sections for examples of display methods: • “The Polynom display Method” on page 9-30 • “The Asset display Method” on page 9-48 • “The Stock display Method” on page 9-57 • “The Portfolio display Method” on page 9-60 Accessing Object Data You need to write methods for your class that provide access to an object’s data. Accessor methods can use a variety of approaches, but all methods that change object data always accept an object as an input argument and return a new object with the data changed. This is necessary because MATLAB does 9-13

Designing User Classes in <strong>MATLAB</strong><br />

Use the whos function to see what objects are in the <strong>MATLAB</strong> workspace.<br />

whos<br />

Name Size Bytes Class<br />

p 1x1 156 polynom object<br />

The display Method<br />

<strong>MATLAB</strong> calls a method named display whenever an object is the result of a<br />

statement that is not terminated by a semicolon. For example, creating the<br />

variable a, which is a double, calls the <strong>MATLAB</strong> display method for doubles.<br />

a = 5<br />

a =<br />

5<br />

You should define a display method so <strong>MATLAB</strong> can display values on the<br />

command line when referencing objects from your class. In many classes,<br />

display can simply print the variable name, and then use the char converter<br />

method to print the contents or value of the variable, since <strong>MATLAB</strong> displays<br />

output as strings. You must define the char method to convert the object’s<br />

data to a character string.<br />

Examples of display Methods<br />

See the following sections for examples of display methods:<br />

• “The Polynom display Method” on page 9-30<br />

• “The Asset display Method” on page 9-48<br />

• “The Stock display Method” on page 9-57<br />

• “The Portfolio display Method” on page 9-60<br />

Accessing Object Data<br />

You need to write methods for your class that provide access to an object’s<br />

data. Accessor methods can use a variety of approaches, but all methods that<br />

change object data always accept an object as an input argument and return a<br />

new object with the data changed. This is necessary because <strong>MATLAB</strong> does<br />

9-13

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

Saved successfully!

Ooh no, something went wrong!