MATLAB Programming
MATLAB Programming MATLAB Programming
9 Classes and Objects visible only within the methods for the class. The design of the appropriate data structure can affect the performance of the code. Tips for C++ and Java Programmers If you are accustomed to programming in other object-oriented languages, such as C++ or Java, you will find that the MATLAB programming language differs from these languages in some important ways: • In MATLAB, method dispatching is not syntax based, as it is in C++ and Java. When the argument list contains objects of equal precedence, MATLAB uses the left-most object to select the method to call. • In MATLAB, there is no equivalent to a destructor method. To remove an object from the workspace, use the clear function. • Construction of MATLAB data types occurs at runtime rather than compile time. You register an object as belonging to a class by calling the class function. • When using inheritance in MATLAB, the inheritance relationship is established in the child class by creating the parent object, and then calling the class function. For more information on writing constructors for inheritance relationships, see “Building on Other Classes” on page 9-38. • When using inheritance in MATLAB, the child object contains a parent object in a property with the name of the parent class. • In MATLAB, there is no passing of variables by reference. When writing methods that update an object, you must pass back the updated object and use an assignment statement. For instance, this call to the set method updates the name field of the object A and returns the updated object. A = set(A,'name','John Smith'); • InMATLAB,thereisnoequivalenttoanabstractclass. • In MATLAB, there is no equivalent to the C++ scoping operator. • In MATLAB, there is no virtual inheritance or virtual base classes. • In MATLAB, there is no equivalent to C++ templates. 9-8
Designing User Classes in MATLAB Designing User Classes in MATLAB This section discusses how to approach the design of a class and describes the basic set of methods that should be included in a class: • “The MATLAB Canonical Class” on page 9-9 • “The Class Constructor Method” on page 9-10 • “Examples of Constructor Methods” on page 9-12 • “Identifying Objects Outside the Class Directory” on page 9-12 • “The display Method” on page 9-13 • “Accessing Object Data” on page 9-13 • “The set and get Methods” on page 9-14 • “Indexed Reference Using subsref and subsasgn” on page 9-15 • “Handling Subscripted Reference” on page 9-16 • “Handling Subscripted Assignment” on page 9-19 • “Object Indexing Within Methods” on page 9-20 • “Defining end Indexing for an Object” on page 9-20 • “Indexing an Object with Another Object” on page 9-21 • “Converter Methods” on page 9-22 The MATLAB Canonical Class When you design a MATLAB class, you should include a standard set of methods that enable the class to behave in a consistent and logical way within the MATLAB environment. Depending on the nature of the class you are defining, you may not need to include all of these methods and you may include a number of other methods to realize the class’s design goals. This table lists the basic methods included in MATLAB classes. 9-9
- Page 618 and 619: 7 Working with Scientific Data Form
- Page 620 and 621: 7 Working with Scientific Data Form
- 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 670 and 671: 9 Classes and Objects Class Method
- Page 672 and 673: 9 Classes and Objects Examples of C
- 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
9 Classes and Objects<br />
visible only within the methods for the class. The design of the appropriate<br />
data structure can affect the performance of the code.<br />
Tips for C++ and Java Programmers<br />
If you are accustomed to programming in other object-oriented languages,<br />
such as C++ or Java, you will find that the <strong>MATLAB</strong> programming language<br />
differs from these languages in some important ways:<br />
• In <strong>MATLAB</strong>, method dispatching is not syntax based, as it is in C++<br />
and Java. When the argument list contains objects of equal precedence,<br />
<strong>MATLAB</strong> uses the left-most object to select the method to call.<br />
• In <strong>MATLAB</strong>, there is no equivalent to a destructor method. To remove an<br />
object from the workspace, use the clear function.<br />
• Construction of <strong>MATLAB</strong> data types occurs at runtime rather than compile<br />
time. You register an object as belonging to a class by calling the class<br />
function.<br />
• When using inheritance in <strong>MATLAB</strong>, the inheritance relationship is<br />
established in the child class by creating the parent object, and then calling<br />
the class function. For more information on writing constructors for<br />
inheritance relationships, see “Building on Other Classes” on page 9-38.<br />
• When using inheritance in <strong>MATLAB</strong>, the child object contains a parent<br />
object in a property with the name of the parent class.<br />
• In <strong>MATLAB</strong>, there is no passing of variables by reference. When writing<br />
methods that update an object, you must pass back the updated object and<br />
use an assignment statement. For instance, this call to the set method<br />
updates the name field of the object A and returns the updated object.<br />
A = set(A,'name','John Smith');<br />
• In<strong>MATLAB</strong>,thereisnoequivalenttoanabstractclass.<br />
• In <strong>MATLAB</strong>, there is no equivalent to the C++ scoping operator.<br />
• In <strong>MATLAB</strong>, there is no virtual inheritance or virtual base classes.<br />
• In <strong>MATLAB</strong>, there is no equivalent to C++ templates.<br />
9-8