logo资料库

Matlab Builder for Java.pdf

第1页 / 共312页
第2页 / 共312页
第3页 / 共312页
第4页 / 共312页
第5页 / 共312页
第6页 / 共312页
第7页 / 共312页
第8页 / 共312页
资料共312页,剩余部分请下载后查看
toc
Getting Started
What Is MATLAB Builder for Java?
Deployment Tool
Deployment Tool Output Window Functionality
Deploying an Application: Magic Square Example
Setting Up Your Environment
Encapsulating Your M-Code in a Java Class
Creating a Deployable Client Component
Testing Your Client Component
Packaging and Distributing Your Java Application
Developing Your Application
Importing Classes
Creating an Instance of the Class
Calling Class Methods from Java
Next Steps
Concepts
What Is a Project?
Classes and Methods
Naming Conventions
How Does MATLAB Builder for Java Handle Data?
Understanding the API Data Conversion Classes
Overview of Classes and Methods in the Data Conversion Class Hie
Advantage of Using Data Conversion Classes
Automatic Conversion to MATLAB Types
Understanding Function Signatures Generated by Java Builder
Understanding MATLAB Function Signatures
Overloaded Methods in Java That Encapsulate M-Code
Returning Data from MATLAB to Java
What Happens in the Build Process?
What Happens in the Package Process?
How Does Component Deployment Work?
Programming
Import Classes
Creating an Instance of the Class
Code Fragment: Instantiating a Java Class
myprimes Function
Passing Arguments to and from Java
Manual Conversion of Data Types
Code Fragment: Using MWNumericArray
Automatic Conversion to a MATLAB Type
Code Fragment: Automatic Data Conversion
Code Fragment: Passing a Java Double Object
Code Fragment: Passing an MWArray
Code Fragment: Calling MWArray Methods
Changing the Default by Specifying the Type
Specifying Optional Arguments
Code Fragment: Passing Variable Numbers of Inputs
Code Fragment: Passing a Variable Number of Outputs
Handling Return Values
Code Fragment: Using Java Reflection
Code Fragment: Using MWArray Query
Code Fragment: Using to Type Array Methods
Passing Java Objects by Reference
Handling Errors
Handling Checked Exceptions
Code Fragment: Handling an Exception in the Called Function
Code Fragment: Handling an Exception in the Calling Function
Handling Unchecked Exceptions
Code Fragment: Catching General Exceptions
Code Fragment: Catching Multiple Exception Types
Managing Native Resources
Using Garbage Collection Provided by the JVM
Using the dispose Method
Code Fragment: Using dispose
Code Fragment: Use try-finally to Ensure Resources Are Freed
Overriding the Object.Finalize Method
Handling Data Conversion Between Java and MATLAB
Calling MWArray Methods
Specifying the Type
Calling Images Methods
Method to Generate a Buffered Image
Input Parameters
Return Values
Exceptions
Setting Java Properties
How to Set Java System Properties
Ensuring a Consistent GUI Appearance
Using Classes and Methods
Guidelines for Working with MWArray Classes
Overview of the MWArray API
Understanding the MWArray Base Class
Accessing Elements of the Arrays
Method Overrides Implemented by MWArray
Java Interfaces Implemented by MWArray
Additional MWArray Methods
Constructing Numeric Arrays
Overview of Constructors and Data Types
Constructing Different Types of Numeric Arrays
Constructing Complex Arrays
Constructing Matrices
Constructing N-Dimensional Arrays
Constructing Jagged Arrays
Using Static Factory Methods to Construct MWNumericArrays
Constructing Sparse Arrays
Accessing MWNumericArray Elements
Working with Logical Arrays
Constructing an MWLogicalArray
Using Static Factory Methods to Create MWLogicalArrays
Accessing MWLogicalArray Elements
Working with Character Arrays
Constructing an MWCharArray
Using Static Factory Methods for Constructing MWCharArrays
Accessing MWCharArray Elements
Working with Cell Arrays
Using MWCellArray Constructors
Accessing MWCellArray Elements
toArray and getData Methods
Working with Struct Arrays
Constructing an MWStructArray
Getting Information About a Structure
Modifying Elements in an MWStructArray
Copying Elements from an MWStructArray
Creating Nested Structures in an MWStructArray
Accessing Elements in an MWStructArray
Using Class Methods
Using MWArray
Constructing an MWArray
Example . Construct an empty MWArray object:
Methods to Create and Destroy an MWArray
Input Parameters
Example — Constructing an MWArray Object
Input Parameters
Example — Constructing an MWNumericArray Object
Methods to Return Information About an MWArray
Input Parameters
Example — Getting the Class ID of an MWArray
Input Parameters
Example — Getting Array Dimensions of an MWArray
Input Parameters
Example — Testing for an Empty MWArray
Input Parameters
Example — Getting the Number of Dimensions of an MWArray
Input Parameters
Example — Getting the Number of MWArray Elements
Methods to Get and Set Data in the MWArray
Input Parameters
Exceptions
Example — Getting an MWArray Value with get
Input Parameters
Example — Getting an MWArray Value with getData
Input Parameters
Exceptions
Example — Setting an MWArray Value
Input Parameters
Example — Getting an MWArray with toArray
Methods to Copy, Convert, and Compare MWArrays
Input Parameters
Exceptions
Example — Cloning an MWArray Object
Input Parameters
Example — Comparing MWArrays with compareTo
Input Parameters
Example — Comparing MWArrays with equals
Input Parameters
Example — Getting an MWArray Hash Code
Input Parameters
Example — Making a Shared Copy of an MWArray
Input Parameters
Example — Converting an MWArray to a String
Methods to Use on Sparse MWArrays
Input Parameters
Example — Testing an MWArray for Sparseness
Input Parameters
Example — Getting the Column Indices of a Sparse MWArray
Input Parameters
Example — Getting the Row Indices of a Sparse MWArray
Input Parameters
Example — Getting the Maximum Number of Nonzeros in an MWArray
Input Parameters.
Example — Getting the Number of Nonzeros in an MWArray
Using MWNumericArray
Constructing an MWNumericArray
Example — Constructing an Empty Numeric Array Object
Exceptions
Example — Constructing an Integer Array Object
Example — Constructing a Complex Array Object
Example — Constructing a Real Array of a Specific Type
Example — Constructing a Complex Array of a Specific Type
Methods to Create and Destroy an MWNumericArray
Input Parameters
Exceptions
Example — Constructing a Numeric Array Object with newInstance
Constructing a Sparse Matrix with No Nonzero Elements
Constructing a Sparse Matrix of Real Numbers
Constructing a Sparse Matrix of Complex Numbers
Input Parameters
Exceptions
Example — Constructing a Sparse Array Object with newSparse
Example — Using newSparse with Row and Column Indices
Example — Assigning Multiple Values to a Single Array Element
Methods to Return Information About an MWNumericArray
Input Parameters
Example — Testing for a Complex Array
Input Parameters
Example — Testing for Finite Array Values
Input Parameters
Example — Testing for Infinite Array Values
Input Parameters
Example — Testing for NaN Array Values
Methods to Get and Set the Real Parts of an MWNumericArray
Example — Getting a Short Value from a Numeric Array
Example — Using get and set on a Numeric Array
Methods to Get and Set the Imaginary Parts of an MWNumericArray
Example — Getting the Real and Imaginary Parts of an Array
Example — Getting Data from a Complex Array
Example — Getting Complex Data of a Specific Type
Exceptions
Input Parameters
Example — Getting Complex Data with toImagArray
Methods to Copy, Convert, and Compare MWNumericArrays
Input Parameters
Exceptions
Example — Cloning a Numeric Array Object
Input Parameters
Example — Making a Shared Copy of a Numeric Array Object
Methods to Use on Sparse MWNumericArrays
Methods to Return Special Constant Values
Input Parameters
Exceptions
Input Parameters
Exceptions
Input Parameters
Exceptions
Methods to Convert Array Data to a Specific Type
Using MWLogicalArray
Constructing an MWLogicalArray
Example — Constructing an Initialized Logical Array Object
Methods to Create and Destroy an MWLogicalArray
Input Parameters
Exceptions
Example — Constructing a Logical Array Object with newInstance
Supported Prototypes
Input Parameters
Exceptions
Example — Constructing a Sparse Logical Array Object
Methods to Return Information About an MWLogicalArray
Input Parameters
Example — Getting the Class ID for a Logical Array Object
Methods to Get and Set Data in an MWLogicalArray
Input Parameters
Exceptions
Example — Getting a Boolean Value from a Logical Array
Input Parameters
Exceptions
Example — Setting a Value in a Logical Array
Methods to Copy, Convert, and Compare MWLogicalArrays
Input Parameters
Exceptions
Example — Cloning a Logical Array Object
Input Parameters
Example — Making a Shared Copy of a Logical Array Object
Methods to Use on Sparse MWLogicalArrays
Using MWCharArray
Constructing an MWCharArray
Input Parameters
Example — Constructing an Initialized Character Array Object
Methods to Create and Destroy an MWCharArray
Input Parameters
Example — Constructing a Character Array Object with newInstance
Methods to Return Information About an MWCharArray
Input Parameters
Example — Getting the Class ID of a Character Array
Methods to Get and Set Data in the MWCharArray
Input Parameters
Exceptions
Example — Getting Character Array Data with getChar
Input Parameters
Exceptions
Example — Setting Values in a Character Array
Methods to Copy, Convert, and Compare MWCharArrays
Input Parameters
Example — Cloning a Character Array Object
Input Parameters
Example — Making a Shared Copy of a Character Array Object
Using MWStructArray
Constructing an MWStructArray
Input Parameters
Example — Constructing a Structure Array Object
Methods to Destroy an MWStructArray
Input Parameters
Example — Disposing of a Structure Array Object
Methods to Return Information About an MWStructArray
Input Parameters
Example — Getting the Class ID of a Structure Array
Input Parameters
Example — Getting the Field Names of a Structure Array
Input Parameters
Example — Getting the Number of Fields in a Structure Array
Methods to Get and Set Data in the MWStructArray
Input Parameters
Exceptions
Example — Getting Structure Array Data with get
Input Parameters
Example — Getting Structure Array Data with getData
Input Parameters
Exceptions
Input Parameters
Exceptions
Example — Setting Values in a Structure Array
Input Parameters
Example — Getting Structure Array Data with toArray
Methods to Copy, Convert, and Compare MWStructArrays
Input Parameters
Exceptions
Example — Cloning a Structure Array Object
Input Parameters
Example — Making a Shared Copy of a Structure Array Object
Using MWCellArray
Constructing an MWCellArray
Input Parameters
Exceptions
Example — Constructing an Empty Cell Array Object
Example — Constructing an Initialized Cell Array Object
Methods to Destroy an MWCellArray
Input Parameters
Example — Disposing of a Cell Array Object
Methods to Return Information About an MWCellArray
Input Parameters
Example — Getting the Class ID of a Cell Array
Methods to Get and Set Data in the MWCellArray
Input Parameters
Exceptions
Example — Getting Data from a Cell Array with get
Input Parameters
Exceptions
Input Parameters
Example — Getting Cell Array Data with getData
Input Parameters
Exceptions
Example — Setting Values in a Cell Array
Input Parameters
Example — Getting Cell Array Data with toArray
Methods to Copy, Convert, and Compare MWCellArrays
Input Parameters
Exceptions
Example — Cloning a Cell Array Object
Input Parameters
Example — Making a Shared Copy of a Cell Array Object
Using MWClassID
Fields of MWClassID
Example — Specifying an MWClassID Value
Using MWComplexity
Fields of MWComplexity
Example – Determining the Complexity of an Array
Sample Java Applications
Plot Example
The purpose of the example is to show you how to do the followin
Plot Example: Step-by-Step Procedure
createplot.java
Spectral Analysis Example
The purpose of the example is to show you the following:
computefft.m
plotfft.m
Spectral Analysis Example: Step-by-Step Procedure
powerspect.java
Matrix Math Example
MATLAB Functions to Be Encapsulated
cholesky.m
ludecomp.m
qrdecomp.m
Step-by-Step Procedure
getfactor.java
Output for the Matrix Math Example
Output for a Sparse Matrix
Understanding the getfactor Program
Phonebook Example
The makephone function takes a structure array as an input, modi
Phonebook Example: Step-by-Step Procedure
getphone.java
Reference Information for Java
Requirements for MATLAB Builder for Java
System Requirements
Limitations and Restrictions
Settings for Environment Variables (Development Machine)
JAVA_HOME Variable
Java CLASSPATH Variable
Native Library Path Variables
Data Conversion Rules
Java to MATLAB Conversion
MATLAB to Java Conversion
Unsupported MATLAB Array Types
Programming Interfaces Generated by Java Builder
APIs Based on MATLAB Function Signatures
Standard API
mlx API
Code Fragment: Signatures Generated for myprimes Example
MWArray Class Specification
Using the Command-Line Interface
Functions — Alphabetical List
Examples
Handling Data
Handling Errors
Handling Memory
Sample Applications (Java)
Index
tables
Overrides
Java Interfaces Implemented by MWArray
Java to MATLAB Conversion Rules
MATLAB to Java Conversion Rules
Using the Command Line to Create Java Components
MATLAB® Builder for Java™ 1 User’s Guide
How to Contact The MathWorks www.mathworks.com comp.soft-sys.matlab www.mathworks.com/contact_TS.html Technical Support Web Newsgroup suggest@mathworks.com bugs@mathworks.com doc@mathworks.com service@mathworks.com info@mathworks.com 508-647-7000 (Phone) 508-647-7001 (Fax) Product enhancement suggestions Bug reports Documentation error reports Order status, license renewals, passcodes Sales, pricing, and general information The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098 For contact information about worldwide offices, see the MathWorks Web site. MATLAB Builder for Java User’s Guide © COPYRIGHT 2006–2007 by The MathWorks, Inc. The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the government’s needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc. Trademarks MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, and xPC TargetBox are registered trademarks, and SimBiology, SimEvents, and SimHydraulics are trademarks of The MathWorks, Inc. Other product or brand names are trademarks or registered trademarks of their respective holders. Patents The MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information. Revision History September 2006 Online only March 2007 Online only New for Version 1.0 (Release 2006b) Revised for Version 1.1 (Release 2007a)
1 2 Contents 开 始 What Is MATLAB Builder for Java? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deployment Tool Deploying an Application: Magic Square Example . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting Up Your Environment Encapsulating Your M-Code in a Java Class . . . . . . . . . . . . Creating a Deployable Client Component . . . . . . . . . . . . . . Testing Your Client Component . . . . . . . . . . . . . . . . . . . . . . 1-2 1-2 1-4 1-4 1-6 1-7 1-8 Packaging and Distributing Your Java Application . . . Developing Your Application . . . . . . . . . . . . . . . . . . . . . . . . 1-10 1-11 Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13 Concepts What Is a Project? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Classes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How Does MATLAB Builder for Java Handle Data? . . . . . . . . . . Understanding the API Data Conversion Classes Automatic Conversion to MATLAB Types . . . . . . . . . . . . . . Understanding Function Signatures Generated by Java Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Returning Data from MATLAB to Java . . . . . . . . . . . . . . . . 2-3 2-3 2-3 2-5 2-5 2-6 2-7 2-8 What Happens in the Build Process? . . . . . . . . . . . . . . . . 2-10 iii
What Happens in the Package Process? . . . . . . . . . . . . . . 2-11 How Does Component Deployment Work? . . . . . . . . . . . . 2-12 3 Programming Import Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating an Instance of the Class . . . . . . . . . . . . . . . . . . . Code Fragment: Instantiating a Java Class . . . . . . . . . . . . Passing Arguments to and from Java . . . . . . . . . . . . . . . . Manual Conversion of Data Types . . . . . . . . . . . . . . . . . . . . Automatic Conversion to a MATLAB Type . . . . . . . . . . . . . Specifying Optional Arguments . . . . . . . . . . . . . . . . . . . . . . Handling Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 3-4 3-4 3-7 3-7 3-8 3-10 3-15 Passing Java Objects by Reference . . . . . . . . . . . . . . . . . . 3-21 Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handling Checked Exceptions . . . . . . . . . . . . . . . . . . . . . . . Handling Unchecked Exceptions . . . . . . . . . . . . . . . . . . . . . Managing Native Resources . . . . . . . . . . . . . . . . . . . . . . . . Using Garbage Collection Provided by the JVM . . . . . . . . . Using the dispose Method . . . . . . . . . . . . . . . . . . . . . . . . . . . Overriding the Object.Finalize Method . . . . . . . . . . . . . . . . Handling Data Conversion Between Java and MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Calling MWArray Methods . . . . . . . . . . . . . . . . . . . . . . . . . . Calling Images Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-22 3-22 3-25 3-28 3-28 3-29 3-30 3-31 3-31 3-32 Setting Java Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . How to Set Java System Properties . . . . . . . . . . . . . . . . . . . 3-34 3-34 iv Contents
4 5 Using Classes and Methods Guidelines for Working with MWArray Classes . . . . . . . Overview of the MWArray API . . . . . . . . . . . . . . . . . . . . . . . Understanding the MWArray Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Constructing Numeric Arrays Working with Logical Arrays . . . . . . . . . . . . . . . . . . . . . . . . Working with Character Arrays . . . . . . . . . . . . . . . . . . . . . . Working with Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with Struct Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 4-2 4-2 4-9 4-23 4-27 4-32 4-38 Using Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-49 Using MWArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-49 Using MWNumericArray . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-70 Using MWLogicalArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-106 Using MWCharArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-122 Using MWStructArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-133 Using MWCellArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-150 Using MWClassID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-164 Using MWComplexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-167 Sample Java Applications Plot Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Spectral Analysis Example . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 5-8 Matrix Math Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . MATLAB Functions to Be Encapsulated . . . . . . . . . . . . . . . Understanding the getfactor Program . . . . . . . . . . . . . . . . . 5-16 5-17 5-26 Phonebook Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-28 v
Reference Information for Java Requirements for MATLAB Builder for Java . . . . . . . . . System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Limitations and Restrictions Settings for Environment Variables (Development Machine) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Data Conversion Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Java to MATLAB Conversion . . . . . . . . . . . . . . . . . . . . . . . . MATLAB to Java Conversion . . . . . . . . . . . . . . . . . . . . . . . . Unsupported MATLAB Array Types . . . . . . . . . . . . . . . . . . Programming Interfaces Generated by Java Builder . . APIs Based on MATLAB Function Signatures . . . . . . . . . . Standard API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . mlx API Code Fragment: Signatures Generated for myprimes Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 6-2 6-2 6-2 6-7 6-7 6-9 6-10 6-11 6-11 6-12 6-14 6-14 MWArray Class Specification . . . . . . . . . . . . . . . . . . . . . . . 6-16 Using the Command-Line Interface . . . . . . . . . . . . . . . . . 6-17 Functions — Alphabetical List Examples Handling Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-2 Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-2 6 7 A vi Contents
Handling Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-2 Sample Applications (Java) . . . . . . . . . . . . . . . . . . . . . . . . . A-3 Index vii
viii Contents
分享到:
收藏