Using MATLAB (779505), страница 5
Текст из файла (страница 5)
20-10Writing Functions to Operate on Structures . . . . . . . . . . . . . 20-11Organizing Data in Structure Arrays . . . . . . . . . . . . . . . . . . . 20-12Nesting Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-17Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .Creating Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Obtaining Data from Cell Arrays . . . . . . . . . . . . . . . . . . . . . .Deleting Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Reshaping Cell Arrays .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Replacing Lists of Variables with Cell Arrays . . . . . . . . . . . .Applying Functions and Operators . . . . . . . . . . . . . . . . . . . . .20-1920-2020-2320-2420-2520-2520-27xviiOrganizing Data in Cell Arrays . . . . .
. . . . . . . . . . . . . . . . . .Nesting Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Converting Between Cell and Numeric Arrays . . . . . . . . . . .Cell Arrays of Structures . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .20-2720-2920-3020-31Function Handles21Benefits of Using Function Handles . . . . . . . . . . . . . . . . . . . . 21-3A Simple Function Handle . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21-5Constructing a Function Handle . . . . . . . .
. . . . . . . . . . . . . . 21-7Maximum Length of a Function Name . . . . . . . . . . . . . . . . . . . 21-7Evaluating a Function Through Its Handle . . . . . . . . . . . . . 21-9Function Evaluation and Overloading . . . . . . . .
. . . . . . . . . . . 21-9Examples of Function Handle Evaluation . . . . . . . . . . . . . . . 21-10Displaying Function Handle Information . . . . . . . . . . . . . 21-13Fields Returned by the Functions Command . . . . . . . . . . . . . 21-14Types of Function Handles .
. . . . . . . . . . . . . . . . . . . . . . . . . . 21-17Function Handle Operations . . . . . . . . . . . . . . . . . . . . . . . . .Converting Function Handles to Function Names . . . . . . . . .Converting Function Names to Function Handles . . . . . . . . .Testing for Data Type . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . .Testing for Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21-2121-2121-2221-2321-23Saving and Loading Function Handles . . . . . . . . . . . . . . .
. 21-25Handling Error Conditions . . . . . . . . . . . . . . . . . . . . . . . . . .Handles to Nonexistent Functions . . . . . . . . . . . . . . . . . . . . .Including Path In the Function Handle Constructor . . . . . . .Evaluating a Nonscalar Function Handle . . . . . .
. . . . . . . . .21-2621-2621-2621-27Historical Note - Evaluating Function Names . . . . . . . . . . 21-28xviii ContentsMATLAB Classes and Objects22Classes and Objects: An Overview . . . . . . . . . . . . . . . . . . . . .Features of Object-Oriented Programming . . . . . .
. . . . . . . . .MATLAB Data Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . .Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Invoking Methods on Objects . . . . . . . . . . . .
. . . . . . . . . . . . . .Private Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Helper Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Debugging Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Setting Up Class Directories . . . . . . . .
. . . . . . . . . . . . . . . . . . .Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Tips for C++ and Java Programmers . . . . . . . . . . . . . . . . . . . .22-322-322-422-422-522-622-622-622-722-822-8Designing User Classes in MATLAB . . . . . . . . . . . . . . . . .
. . . 22-9The MATLAB Canonical Class . . . . . . . . . . . . . . . . . . . . . . . . . 22-9The Class Constructor Method . . . . . . . . . . . . . . . . . . . . . . . . 22-10Examples of Constructor Methods . . . . . . . . . . . . . . . . . . . . . 22-11Identifying Objects Outside the Class Directory . . . . . . . . . . 22-11The display Method . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-12Accessing Object Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-13The set and get Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-13Indexed Reference Using subsref and subsasgn . . . . . . . . . . 22-14Handling Subscripted Reference .
. . . . . . . . . . . . . . . . . . . . . . 22-14Handling Subscripted Assignment . . . . . . . . . . . . . . . . . . . . . 22-17Object Indexing Within Methods . . . . . . . . . . . . . . . . . . . . . . 22-17Defining end Indexing for an Object .
. . . . . . . . . . . . . . . . . . . 22-18Indexing an Object with Another Object . . . . . . . . . . . . . . . . 22-19Converter Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-20Overloading Operators and Functions . . . . . . . . .
. . . . . . . 22-21Overloading Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-21Overloading Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-23Example - A Polynomial Class . . . . . . . . . . . . . . . . . . . . . . . .Polynom Data Structure .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .Polynom Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Polynom Constructor Method . . . . . . . . . . . . . . . . . . . . .Converter Methods for the Polynom Class . .
. . . . . . . . . . . . .22-2422-2422-2422-2422-25xixThe Polynom display Method . . . . . . . . . . . . . . . . . . . . . . . . .The Polynom subsref Method . . . . . . . . . . . . . . . . . . . . . . . . .Overloading Arithmetic Operators for polynom . . . .
. . . . . . .Overloading Functions for the Polynom Class . . . . . . . . . . . .Listing Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22-2822-2822-2922-3122-33Building on Other Classes . . . . . . . . . . . .
. . . . . . . . . . . . . . .Simple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22-3522-3522-3622-37Example - Assets and Asset Subclasses . .
. . . . . . . . . . . . . .Inheritance Model for the Asset Class . . . . . . . . . . . . . . . . . .Asset Class Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Other Asset Methods . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .The Asset Constructor Method . . . . . . . . . . . . . . . . . . . . . . . .The Asset get Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Asset set Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Asset subsref Method . . . . . .
. . . . . . . . . . . . . . . . . . . . . .The Asset subsasgn Method . . . . . . . . . . . . . . . . . . . . . . . . . .The Asset display Method . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Asset fieldcount Method . . . . . . . . . . . . . . . . .
. . . . . . . . .Designing the Stock Class . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Stock Constructor Method . . . . . . . . . . . . . . . . . . . . . . . .The Stock get Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Stock set Method . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .The Stock subsref Method . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Stock subsasgn Method . . . . . . . . . . . . . . . . . . . . . . . . . .The Stock display Method . . . . . . . . . . . . . . . . . . . . . .
. . . . . .22-3822-3822-3922-3922-3922-4122-4122-4222-4322-4422-4522-4522-4622-4822-4822-4922-5122-52Example - The Portfolio Container . . . . . . . . . . . . . . . . . . . .Designing the Portfolio Class . . . . . . . . . . . . . . . . . . . . . . . . .The Portfolio Constructor Method . . . . . . . . . . . . . . . . . . . . .The Portfolio display Method .
. . . . . . . . . . . . . . . . . . . . . . . .The Portfolio pie3 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating a Portfolio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22-5422-5422-5522-5622-5722-58Saving and Loading Objects . .
. . . . . . . . . . . . . . . . . . . . . . . . 22-60Modifying Objects During Save or Load . . . . . . . . . . . . . . . . . 22-60xxContentsExample - Defining saveobj and loadobj for Portfolio . . .Summary of Code Changes . . . . . . . . . . . . . . . . . . . . . . . . . . .The saveobj Method . . . . . . .
















