MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 622 and 623: 7 Working with Scientific Data Form
- Page 624 and 625: 7 Working with Scientific Data Form
- Page 626 and 627: 7 Working with Scientific Data Form
- Page 628 and 629: 7 Working with Scientific Data Form
- Page 630 and 631: 7 Working with Scientific Data Form
- Page 632 and 633: 7 Working with Scientific Data Form
- Page 634 and 635: 7 Working with Scientific Data Form
- Page 636 and 637: 8 Error Handling Checking for Error
- Page 638 and 639: 8 Error Handling Handling and Recov
- Page 640 and 641: 8 Error Handling X = A * B catch di
- Page 642 and 643: 8 Error Handling matrixMultiply(A,
- Page 644 and 645: 8 Error Handling Message Identifier
- Page 646 and 647: 8 Error Handling error('msg_id', 'e
- Page 648 and 649: 8 Error Handling Warnings Like erro
- Page 650 and 651: 8 Error Handling Warning Control Th
- Page 652 and 653: 8 Error Handling warnings by issuin
- Page 654 and 655: 8 Error Handling You must type the
- Page 656 and 657: 8 Error Handling s(2) ans = identif
- Page 658 and 659: 8 Error Handling end function f2(x)
- Page 660 and 661: 8 Error Handling Debugging Errors a
- Page 662 and 663: 9 Classes and Objects Classes and O
- Page 664 and 665: 9 Classes and Objects The diagram s
- Page 666 and 667: 9 Classes and Objects functions do
- Page 668 and 669: 9 Classes and Objects visible only
- Page 670 and 671: 9 Classes and Objects Class Method
- Page 674 and 675: 9 Classes and Objects not support p
- Page 676 and 677: 9 Classes and Objects overloaded me
- Page 678 and 679: 9 Classes and Objects A(1,2).name(3
- Page 680 and 681: 9 Classes and Objects Object Indexi
- Page 682 and 683: 9 Classes and Objects Converter Met
- Page 684 and 685: 9 Classes and Objects Operation M-F
- Page 686 and 687: 9 Classes and Objects Example — A
- Page 688 and 689: 9 Classes and Objects to the .c fie
- Page 690 and 691: 9 Classes and Objects end if d >= 2
- Page 692 and 693: 9 Classes and Objects % SUBSREF swi
- Page 694 and 695: 9 Classes and Objects q = polynom(q
- Page 696 and 697: 9 Classes and Objects % POLYNOM/DIF
- Page 698 and 699: 9 Classes and Objects Building on O
- Page 700 and 701: 9 Classes and Objects Multiple Inhe
- Page 702 and 703: 9 Classes and Objects Inheritance M
- Page 704 and 705: 9 Classes and Objects a.currentValu
- Page 706 and 707: 9 Classes and Objects error('Asset
- Page 708 and 709: 9 Classes and Objects case 'date' a
- Page 710 and 711: 9 Classes and Objects Note that the
- Page 712 and 713: 9 Classes and Objects For example,
- Page 714 and 715: 9 Classes and Objects The Stock sub
- Page 716 and 717: 9 Classes and Objects case '.' swit
- Page 718 and 719: 9 Classes and Objects Example — T
- Page 720 and 721: 9 Classes and Objects if isa(name,'
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