MATLAB Object-Oriented Programming

(Joyce) #1



    • Why Use Object-Oriented Design........................ 1-

      • Approaches to Writing MATLAB Programs............... 1-

      • When Should You Create Object-Oriented Programs........ 1-



    • Handle Object Behavior 1-

      • What Is a Handle?................................. 1-

      • Copies of Handles.................................. 1-

      • Handle Objects Modified in Functions.................. 1-

      • Determine If an Object Is a Handle.................... 1-

      • Deleted Handle Objects............................ 1-





  • Basic Example

    • Create a Simple Class................................. 2-

      • Design Class...................................... 2-

      • Create Object..................................... 2-

      • Access Properties.................................. 2-

      • Call Methods..................................... 2-

      • Add Constructor................................... 2-

      • Vectorize Methods................................. 2-

      • Overload Functions................................ 2-

      • BasicClass Code Listing 2-





  • MATLAB Classes Overview

    • Role of Classes in MATLAB 3-

      • Classes 3-

      • Some Basic Relationships 3-



    • Developing Classes — Typical Workflow................... 3-

      • Formulating a Class 3-

      • Specifying Class Components 3-

      • BankAccount Class Implementation................... 3-

      • Formulating the AccountManager Class................ 3-

      • Implementing the AccountManager Class............... 3-

      • AccountManager Class Synopsis...................... 3-

      • Using BankAccount Objects 3-



    • Representing Structured Data with Classes.............. 3-

      • Objects as Data Structures 3-

      • Structure of the Data.............................. 3-

      • The TensileData Class.............................. 3-

      • Create an Instance and Assign Data................... 3-

      • Restrict Properties to Specific Values.................. 3-

      • Simplifying the Interface with a Constructor............. 3-

      • Calculate Data on Demand 3-

      • Displaying TensileData Objects....................... 3-

      • Method to Plot Stress vs. Strain...................... 3-

      • TensileData Class Synopsis.......................... 3-



    • Implementing Linked Lists with Classes................. 3-

      • Class Definition Code 3-

      • dlnode Class Design............................... 3-

      • Create Doubly Linked List.......................... 3-

      • Why a Handle Class for Linked Lists?.................. 3-

      • dlnode Class Synopsis 3-

      • Specialize the dlnode Class.......................... 3-





  • Static Data

    • Static Data 4-

      • What Is Static Data................................. 4-

      • Static Variable.................................... 4-

      • Static Data Object................................. 4-

      • Constant Data 4-





  • Class Definition—Syntax Reference

    • Class Files and Folders................................ 5-

      • Class Definition Files 5-

      • Options for Class Folders 5-

      • Options for Class Files 5-

      • Group Classes with Package Folders.................... 5-



    • Class Components.................................... 5-

      • Class Building Blocks 5-

      • Class Definition Block 5-

      • Properties Block................................... 5-

      • Methods Block.................................... 5-

      • Events Block 5-

      • A Complete Class 5-

      • Enumeration Classes............................... 5-

      • Related Information................................ 5-



    • Classdef Block 5-

      • How to Specify Attributes and Superclasses 5-

      • Class Attribute Syntax 5-

      • Superclass Syntax................................ 5-

      • Local Functions in Class File........................ 5-



    • Properties.......................................... 5-

      • The Properties Block.............................. 5-

      • Access to Property Values........................... 5-





  • Methods and Functions............................... 5-

    • The Methods Block................................ 5-

    • Method Calling Syntax 5-

    • Private Methods 5-

    • More Detailed Information On Methods................ 5-

    • Class-Related Functions 5-

    • How to Overload Functions and Operators.............. 5-

    • Rules for Defining Methods in Separate Files............ 5-



  • Events and Listeners 5-

    • Define and Trigger Events.......................... 5-

    • Listen for Events................................. 5-



  • Attribute Specification 5-

    • Attribute Syntax 5-

    • Attribute Descriptions............................. 5-

    • Attribute Values.................................. 5-

    • Simpler Syntax for true/false Attributes................ 5-



  • Call Superclass Methods on Subclass Objects 5-

    • Superclass Relation to Subclass...................... 5-

    • How to Call Superclass Methods...................... 5-

    • How to Call Superclass Constructor................... 5-



  • Representative Class Code............................ 5-

    • Class Calculates Area 5-

    • Description of Class Definition....................... 5-



  • MATLAB Code Analyzer Warnings...................... 5-

    • Syntax Warnings and Property Names................. 5-

    • Variable/Property Name Conflict Warnings.............. 5-

    • Exception to Variable/Property Name Rule.............. 5-



  • Objects In Conditional Statements..................... 5-

    • Enable Use of Objects In Conditional Statements......... 5-

    • How MATLAB Evaluates Switch Statements 5-

    • How to Define the eq Method........................ 5-

    • Enumerations in Switch Statements 5-



  • Operations on Objects 5-

    • Object Operations 5-

    • Help on Objects 5-

    • Functions to Test Objects........................... 5-

      • Functions to Query Class Components................. 5-



    • Use of Editor and Debugger with Classes................ 5-

      • Write Class Code in the Editor....................... 5-

      • How to Refer to Class Files.......................... 5-

      • How to Debug Class Files........................... 5-



    • Automatic Updates for Modified Classes................. 5-

      • When MATLAB Loads Class Definitions................. 5-

      • Consequences of Automatic Update................... 5-

      • What Happens When Class Definitions Change........... 5-

      • Actions That Do Not Trigger Updates.................. 5-

      • Multiple Updates to Class Definitions 5-

      • Object Validity with Deleted Class File 5-

      • When Updates Are Not Possible...................... 5-

      • Potential Consequences of Class Updates............... 5-

      • Updates to Class Attributes......................... 5-

      • Updates to Property Definitions...................... 5-

      • Updates to Method Definitions 5-

      • Updates to Event Definitions 5-



    • Compatibility with Previous Versions................... 5-

      • Version 7.6.................................... 5- New Class-Definition Syntax Introduced with MATLAB Software

      • Changes to Class Constructors....................... 5-

      • New Features Introduced with Version 7.6.............. 5-

      • Examples of Old and New........................... 5-



    • Comparison of MATLAB and Other OO Languages......... 5-

      • Some Differences from C++ and Java Code............. 5-

      • Object Modification............................... 5-

      • Static Properties.................................. 5-

      • Common Object-Oriented Techniques.................. 5-





  • Defining and Organizing Classes

    • User-Defined Classes 6-

      • What Is a Class Definition............................ 6-

      • Attributes for Class Members......................... 6-



    • Kinds of Classes................................... 6-

    • Constructing Objects............................... 6-

    • Class Hierarchies.................................. 6-

    • classdef Syntax.................................... 6-

    • Class Code....................................... 6-



  • Class Attributes...................................... 6-

    • Specifying Class Attributes........................... 6-

    • Specifying Attributes............................... 6-

    • Class-Specific Attributes............................. 6-



  • Evaluation of Expressions in Class Definitions 6-

    • Why Use Expressions 6-

    • Where to Use Expressions in Class Definitions 6-

    • How MATLAB Evaluates Expressions 6-

    • When MATLAB Evaluates Expressions................. 6-

    • Expression Evaluation in Handle and Value Classes....... 6-



  • Folders Containing Class Definitions.................... 6-

    • Class Definitions On the Path........................ 6-

    • Class and Path Folders 6-

    • Using Path Folders................................ 6-

    • Using Class Folders............................... 6-

    • Functions in Private Folders Within Class Folders......... 6-

    • Class Precedence and MATLAB Path................... 6-

    • Changing Path to Update Class Definition............... 6-



  • Class Precedence.................................... 6-

    • Use of Class Precedence 6-

    • Why Mark Classes as Inferior........................ 6-

    • InferiorClasses Attribute 6-



  • Packages Create Namespaces 6-

    • Package Folders.................................. 6-

    • Internal Packages................................. 6-

    • Referencing Package Members Within Packages.......... 6-

    • Referencing Package Members from Outside the Package 6-

    • Packages and the MATLAB Path 6-



  • Import Classes 6-

    • Syntax for Importing Classes........................ 6-

    • Import Package Functions.......................... 6-

    • Package Function and Class Method Name Conflict....... 6-

      • Clearing Import List............................... 6-





  • Value or Handle Class — Which to Use

    • Comparison of Handle and Value Classes................. 7-

      • Basic Difference................................... 7-

      • Behavior of MATLAB Built-In Classes................... 7-

      • User-Defined Value Classes........................... 7-

      • User-Defined Handle Classes......................... 7-

      • Determining Equality of Objects 7-

      • Functionality Supported by Handle Classes............. 7-



    • Which Kind of Class to Use............................ 7-

      • Examples of Value and Handle Classes 7-

      • When to Use Value Classes 7-

      • When to Use Handle Classes 7-



    • The Handle Superclass 7-

      • Building on the Handle Class........................ 7-

      • Handle Class Methods............................. 7-

      • Event and Listener Methods 7-

      • Relational Methods................................ 7-

      • Test Handle Validity 7-

      • When MATLAB Destroys Objects..................... 7-



    • Handle Class Destructor 7-

      • Basic Knowledge................................. 7-

      • Syntax of Handle Class Destructor Method.............. 7-

      • Handle Object During delete Method Execution.......... 7-

      • Support Destruction of Partially Constructed Objects...... 7-

      • When to Define a Destructor Method.................. 7-

      • Destructors in Class Hierarchies...................... 7-

      • Object Lifecycle.................................. 7-

      • Restrict Access to Object Delete Method 7-

      • Nondestructor Delete Methods....................... 7-

      • Java Objects Referencing MATLAB Objects.............. 7-



    • Find Handle Objects and Properties.................... 7-

      • Find Handle Objects............................... 7-

      • Find Handle Object Properties....................... 7-



    • Implement Set/Get Interface for Properties 7-

      • The Standard Set/Get Interface...................... 7-

      • Subclass Syntax 7-

      • Get Method Syntax................................ 7-

      • Set Method Syntax................................ 7-

      • Class Derived from matlab.mixin.SetGet................ 7-



    • Implement Copy for Handle Classes.................... 7-

      • Copy Method for Handle Classes..................... 7-

      • Customize Copy Operation 7-

      • Copy Properties That Contain Handles 7-

      • Exclude Properties from Copy 7-





  • Properties — Storing Class Data

    • Ways to Use Properties 8-

      • What Are Properties 8-

      • Types of Properties................................. 8-



    • Property Syntax 8-

      • Property Definition Block............................ 8-

      • Access Property Values 8-

      • Inheritance of Properties............................ 8-

      • Specify Property Attributes........................... 8-



    • Property Attributes................................... 8-

      • Purpose of Property Attributes 8-

      • Specifying Property Attributes 8-

      • Table of Property Attributes.......................... 8-



    • Property Definition.................................. 8-

      • What You Can Define.............................. 8-

      • Initialize Property Values........................... 8-

      • Property Default Values............................ 8-

      • Assign Property Values from Constructor 8-

      • Initialize Properties to Unique Values.................. 8-

      • Property Attributes................................ 8-



    • Property Access Methods 8-

    • Reference Object Properties Using Variables............ 8-



  • Mutable and Immutable Properties..................... 8-

    • Set Access to Property Values........................ 8-

    • Define Immutable Property.......................... 8-



  • Validate Property Values 8-

    • Property Validation in Class Definitions 8-

    • Sample Class Using Property Validation................ 8-

    • Order of Validation................................ 8-

    • Abstract Property Validation......................... 8-

    • Objects Not Updated When Changing Validation.......... 8-

    • Validation During Load Operation 8-



  • Property Class and Size Validation...................... 8-

    • Property Class and Size............................ 8-

    • Property Size Validation 8-

    • Property Class Validation 8-

    • Default Values Per Size and Class..................... 8-



  • Property Validation Functions......................... 8-

    • Validate Property Using Functions.................... 8-

    • MATLAB Validation Functions........................ 8-

    • Define Validation Functions......................... 8-

    • Add Support for Validation Functions.................. 8-



  • Metadata Interface to Property Validation 8-

  • Property Access Methods 8-

    • Properties Provide Access to Class Data................ 8-

    • Property Setter and Getter Methods................... 8-

    • Set and Get Method Execution and Property Events....... 8-

    • Access Methods and Properties Containing Arrays........ 8-

    • Access Methods and Arrays of Objects 8-

    • Modify Property Values with Access Methods 8-



  • Property Set Methods................................ 8-

    • Overview of Property Access Methods................. 8-

    • Property Set Method Syntax......................... 8-

    • Validate Property Set Value 8-

    • When Set Method Is Called.......................... 8-

    • Property Get Methods 8-

      • Overview of Property Access Methods................. 8-

      • Property Get Method Syntax......................... 8-

      • Calculate Value for Dependent Property 8-

      • Errors Not Returned from Get Method 8-

      • Get Method Behavior.............................. 8-



    • Set and Get Methods for Dependent Properties........... 8-

      • Calculate Dependent Property Value 8-

      • When to Use Set Methods with Dependent Properties...... 8-

      • Private Set Access with Dependent Properties........... 8-



    • Properties Containing Objects......................... 8-

      • Assigning Objects as Default Property Values 8-

      • Assigning to Read-Only Properties Containing Objects..... 8-

      • Assignment Behavior.............................. 8-



    • Dynamic Properties — Adding Properties to an Instance... 8-

      • What Are Dynamic Properties 8-

      • Define Dynamic Properties.......................... 8-



    • Set and Get Methods for Dynamic Properties 8-

      • Create Access Methods for Dynamic Properties 8-

      • Shared Set and Get Methods........................ 8-



    • Dynamic Property Events 8-

      • Dynamic Properties and Ordinary Property Events 8-

      • Dynamic-Property Events........................... 8-

      • Listen for a Specific Property Name................... 8-

      • PropertyAdded Event Callback Execution............... 8-

      • PropertyRemoved Event Callback Execution............. 8-

      • How to Find meta.DynamicProperty Objects............. 8-



    • Dynamic Properties and ConstructOnLoad 8-



  • Methods — Defining Class Operations

    • Methods in Class Design 9-

      • Class Methods.................................... 9-



    • Examples and Syntax............................... 9-

    • Kinds of Methods.................................. 9-

    • Method Naming 9-



  • Method Attributes.................................... 9-

    • Purpose of Method Attributes......................... 9-

    • Specifying Method Attributes......................... 9-

    • Table of Method Attributes........................... 9-



  • Ordinary Methods 9-

    • Ordinary Methods Operate on Objects.................. 9-

    • Methods Inside classdef Block........................ 9-

    • Method Files..................................... 9-



  • Methods in Separate Files............................ 9-

    • Class Folders 9-

    • Define Method in Function File....................... 9-

    • Specify Method Attributes in classdef File.............. 9-

    • Methods You Must Define in the classdef File............ 9-



  • Method Invocation 9-

    • Determining Which Method Is Invoked 9-

    • Referencing Names with Expressions—Dynamic Reference.. 9-

    • Controlling Access to Methods....................... 9-

    • Invoking Superclass Methods in Subclass Methods 9-

    • Invoking Built-In Functions.......................... 9-



  • Class Constructor Methods............................ 9-

    • Purpose of Class Constructor Methods 9-

    • Basic Structure of Constructor Methods................ 9-

    • Guidelines for Constructors......................... 9-

    • Default Constructor 9-

    • When to Define Constructors........................ 9-

    • Related Information............................... 9-

    • Initializing Objects in Constructor.................... 9-

    • No Input Argument Constructor Requirement............ 9-

    • Subclass Constructors 9-

    • Implicit Call to Inherited Constructor.................. 9-

    • Errors During Class Construction 9-

    • Output Object Suppressed.......................... 9-



  • Static Methods 9-

    • What Are Static Methods........................... 9-

      • Why Define Static Methods.......................... 9-

      • Defining Static Methods............................ 9-

      • Calling Static Methods............................. 9-

      • Inheriting Static Methods........................... 9-



    • Overload Functions in Class Definitions 9-

      • Why Overload Functions............................ 9-

      • Implementing Overloaded MATLAB Functions 9-

      • Rules for Naming to Avoid Conflicts................... 9-



    • Class Support for Array-Creation Functions.............. 9-

      • Extend Array-Creation Functions for Your Class.......... 9-

      • Which Syntax to Use............................... 9-

      • Implement Support for Array-Creation Functions......... 9-



    • Object Precedence in Method Invocation 9-

      • Object Precedence................................ 9-

      • Defining Precedence............................... 9-



    • Dominant Argument in Overloaded Graphics Functions.... 9-

      • Graphics Object Precedence......................... 9-

      • Dominant Argument............................... 9-

      • Defining Class Precedence.......................... 9-

      • Calls to Inferior-Class Methods 9-



    • Class Methods for Graphics Callbacks 9-

      • Referencing the Method 9-

      • Syntax for Method Callbacks........................ 9-

      • Use a Class Method for a Slider Callback............... 9-





  • Object Arrays

    • Construct Object Arrays 10-

      • Build Arrays in the Constructor...................... 10-

      • Referencing Property Values in Object Arrays............ 10-



    • Initialize Object Arrays............................... 10-

      • Calls to Constructor............................... 10-

      • Initial Value of Object Properties..................... 10-





  • Empty Arrays....................................... 10-

    • Creating Empty Arrays 10-

    • Assigning Values to an Empty Array................... 10-



  • Initialize Arrays of Handle Objects 10-

    • Related Information.............................. 10-



  • Accessing Dynamic Properties in Arrays................ 10-

  • Implicit Class Conversion............................ 10-

    • Class Conversion Mechanism....................... 10-

    • Concatenation 10-

    • Subscripted Assignment 10-



  • Concatenating Objects of Different Classes............. 10-

    • Basic Knowledge................................ 10-

    • MATLAB Concatenation Rules...................... 10-

    • Concatenating Objects............................ 10-

    • Calling the Dominant-Class Constructor............... 10-

    • Converter Methods............................... 10-



  • Designing Heterogeneous Class Hierarchies............ 10-

    • Creating Classes That Support Heterogeneous Arrays.... 10-

    • MATLAB Arrays 10-

    • Heterogeneous Hierarchies 10-

    • Heterogeneous Arrays............................ 10-

    • Heterogeneous Array Concepts..................... 10-

    • Nature of Heterogeneous Arrays.................... 10-

    • Unsupported Hierarchies 10-

    • Default Object 10-

    • Conversion During Assignment and Concatenation 10-

    • Empty Arrays of Heterogeneous Abstract Classes........ 10-



  • Heterogeneous Array Constructors 10-

    • Building Arrays in Superclass Constructors............ 10-

    • When Errors Can Occur........................... 10-

    • Initialize Array in Superclass Constructor 10-

    • Sample Implementation 10-

    • Potential Error 10-



  • Events — Sending and Responding to Messages

    • Overview Events and Listeners......................... 11-

      • Why Use Events and Listeners 11-

      • Events and Listeners Basics......................... 11-

      • Events and Listeners Syntax......................... 11-

      • Create Listener 11-



    • Define Custom Event Data............................ 11-

      • Class Event Data Requirements...................... 11-

      • Define and Trigger Event........................... 11-

      • Define Event Data................................ 11-

      • Create Listener for Overflow Event.................... 11-



    • Observe Changes to Property Values.................... 11-

    • Implement Property Set Listener 11-

      • PushButton Class Design.......................... 11-



    • Event and Listener Concepts......................... 11-

      • The Event Model................................ 11-

      • Limitations..................................... 11-

      • Default Event Data 11-

      • Events Only in Handle Classes 11-

      • Property-Set and Query Events 11-

      • Listeners 11-



    • Event Attributes.................................... 11-

      • Specify Event Attributes........................... 11-



    • Events and Listeners Syntax.......................... 11-

      • Components to Implement 11-

      • Name Events................................... 11-

      • Trigger Events 11-

      • Listen to Events................................. 11-

      • Define Event-Specific Data 11-



    • Listener Lifecycle 11-

      • Control Listener Lifecycle.......................... 11-

      • Temporarily Deactivate Listeners.................... 11-

      • Permanently Delete Listeners 11-





  • Listener Callback Syntax 11-

    • Specifying Listener Callbacks....................... 11-

    • Input Arguments for Callback Function 11-

    • Additional Arguments for Callback Function............ 11-



  • Callback Execution................................. 11-

    • When Callbacks Execute........................... 11-

    • Listener Order of Execution........................ 11-

    • Callbacks That Call notify.......................... 11-

    • Manage Callback Errors........................... 11-

    • Invoke Functions from Function Handles.............. 11-



  • Determine If Event Has Listeners..................... 11-

    • Do Listeners Exist for This Event?................... 11-

    • Why Test for Listeners............................ 11-

    • Coding Patterns................................. 11-

    • Listeners in Heterogeneous Arrays................... 11-



  • Listen for Changes to Property Values................. 11-

    • Create Property Listeners.......................... 11-

    • Property Event and Listener Classes.................. 11-



  • Assignment When Property Value Is Unchanged 11-

    • AbortSet When Value Does Not Change 11-

    • How MATLAB Compares Values..................... 11-

    • When to Use AbortSet............................ 11-

    • Implement AbortSet.............................. 11-

    • Using AbortSet with Property Validation............... 11-



  • Techniques for Using Events and Listeners 11-

    • Example Overview 11-

    • Techniques Demonstrated in This Example 11-

    • Summary of fcneval Class 11-

    • Summary of fcnview Class......................... 11-

    • Methods Inherited from Handle Class 11-

    • Using the fcneval and fcnview Classes................ 11-

    • Implement UpdateGraph Event and Listener........... 11-

    • The PostSet Event Listener 11-

    • Enable and Disable Listeners....................... 11-

    • @fcneval/fcneval.m Class Code 11-

    • @fcnview/fcnview.m Class Code..................... 11-



  • How to Build on Other Classes

    • Hierarchies of Classes — Concepts 12-

      • Classification 12-

      • Develop the Abstraction 12-

      • Design of Class Hierarchies......................... 12-

      • Super and Subclass Behavior........................ 12-

      • Implementation and Interface Inheritance 12-



    • Subclass Syntax..................................... 12-

      • Subclass Definition Syntax.......................... 12-

      • Subclass double.................................. 12-



    • Design Subclass Constructors......................... 12-

      • Call Superclass Constructor Explicitly................. 12-

      • Call Superclass Constructor from Subclass.............. 12-

      • Subclass Constructor Implementation................ 12-

      • Call Only Direct Superclass from Constructor........... 12-



    • Control Sequence of Constructor Calls 12-

    • Modify Inherited Methods........................... 12-

      • When to Modify Superclass Methods................. 12-

      • Extend Superclass Methods 12-

      • Redefine Superclass Methods in Subclass.............. 12-

      • Override Superclass Methods....................... 12-



    • Modify Inherited Properties.......................... 12-

      • Superclass Property Modification.................... 12-

      • Private Local Property Takes Precedence in Method 12-



    • Subclassing Multiple Classes......................... 12-

      • Specify Multiple Superclasses 12-

      • Class Member Compatibility........................ 12-

      • Multiple Inheritance.............................. 12-



    • Specify Allowed Subclasses 12-

      • Basic Knowledge................................ 12-

      • Why Control Allowed Subclasses.................... 12-

      • Specify Allowed Subclasses........................ 12-

      • Define Sealed Hierarchy of Classes................... 12-





Free download pdf