MATLAB External Interfaces COPYRIGHT 1984 - 2001 by the Mathworks, Inc
Total Page:16
File Type:pdf, Size:1020Kb
Computation Visualization Programming External Interfaces Version 6 How to Contact The MathWorks: www.mathworks.com Web comp.soft-sys.matlab Newsgroup [email protected] Technical support [email protected] Product enhancement suggestions [email protected] Bug reports [email protected] Documentation error reports [email protected] Order status, license renewals, passcodes [email protected] Sales, pricing, and general information 508-647-7000 Phone 508-647-7001 Fax The MathWorks, Inc. Mail 3 Apple Hill Drive Natick, MA 01760-2098 For contact information about worldwide offices, see the MathWorks Web site. MATLAB External Interfaces COPYRIGHT 1984 - 2001 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 repro- duced 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 or for the federal government of the United States. By accepting delivery of the Program, the government hereby agrees that this software qualifies as "commercial" computer software within the meaning of FAR Part 12.212, DFARS Part 227.7202-1, DFARS Part 227.7202-3, DFARS Part 252.227-7013, and DFARS Part 252.227-7014. The terms and conditions of The MathWorks, Inc. Software License Agreement shall pertain to the government’s use and disclosure of the Program and Documentation, and shall supersede any conflicting contractual terms or conditions. If this license fails to meet the government’s minimum needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to MathWorks. MATLAB, Simulink, Stateflow, Handle Graphics, and Real-Time Workshop are registered trademarks, and Target Language Compiler is a trademark of The MathWorks, Inc. Other product or brand names are trademarks or registered trademarks of their respective holders. Printing History: December 1996 First printing July 1997 Revised for 5.1 (online only) January 1998 Second printing Revised for MATLAB 5.2 October 1998 Third printing Revised for MATLAB 5.3 (Release 11) November 2000 Fourth printing Revised and renamed for MATLAB 6.0 (Release 12) June 2001 Online only Revised for MATLAB 6.1 (Release 12.1) Contents Calling C and Fortran Programs from MATLAB 1 Introducing MEX-Files . 1-3 Using MEX-Files . 1-3 The Distinction Between mx and mex Prefixes . 1-4 MATLAB Data . 1-6 The MATLAB Array . 1-6 Data Storage . 1-6 Data Types in MATLAB . 1-7 Using Data Types . 1-9 Building MEX-Files . 1-11 Compiler Requirements . 1-11 Testing Your Configuration on UNIX . 1-12 Testing Your Configuration on Windows . 1-14 Specifying an Options File . 1-17 Custom Building MEX-Files . 1-20 Who Should Read This Chapter . 1-20 MEX Script Switches . 1-20 Default Options File on UNIX . 1-22 Default Options File on Windows . 1-23 Custom Building on UNIX . 1-24 Custom Building on Windows . 1-26 Troubleshooting . 1-32 Configuration Issues . 1-32 Understanding MEX-File Problems . 1-33 Compiler and Platform-Specific Issues . 1-37 Memory Management Compatibility Issues . 1-37 Additional Information . 1-42 Files and Directories - UNIX Systems . 1-42 Files and Directories - Windows Systems . 1-44 i Examples . 1-46 Technical Support . 1-48 Creating C Language MEX-Files 2 C MEX-Files . 2-3 The Components of a C MEX-File . 2-3 Required Arguments to a MEX-File . 2-5 Examples of C MEX-Files . 2-7 A First Example — Passing a Scalar . 2-7 Passing Strings . 2-11 Passing Two or More Inputs or Outputs . 2-13 Passing Structures and Cell Arrays . 2-16 Handling Complex Data . 2-21 Handling 8-,16-, and 32-Bit Data . 2-23 Manipulating Multidimensional Numerical Arrays . 2-25 Handling Sparse Arrays . 2-29 Calling Functions from C MEX-Files . 2-33 Advanced Topics . 2-37 Help Files . 2-37 Linking Multiple Files . 2-37 Workspace for MEX-File Functions . 2-37 Memory Management . 2-38 Using LAPACK and BLAS Functions . 2-40 Debugging C Language MEX-Files . 2-47 Debugging on UNIX . 2-47 Debugging on Windows . 2-48 ii Contents Creating Fortran MEX-Files 3 Fortran MEX-Files . 3-3 The Components of a Fortran MEX-File . 3-3 The %val Construct . 3-8 Examples of Fortran MEX-Files . 3-9 A First Example — Passing a Scalar . 3-10 Passing Strings . 3-12 Passing Arrays of Strings . 3-14 Passing Matrices . 3-17 Passing Two or More Inputs or Outputs . 3-19 Handling Complex Data . 3-22 Dynamically Allocating Memory . 3-25 Handling Sparse Matrices . 3-28 Calling Functions from Fortran MEX-Files . 3-32 Advanced Topics . 3-36 Help Files . 3-36 Linking Multiple Files . 3-36 Workspace for MEX-File Functions . 3-36 Memory Management . 3-37 Debugging Fortran Language MEX-Files . 3-38 Debugging on UNIX . 3-38 Debugging on Windows . 3-39 Calling MATLAB from C and Fortran Programs 4 Using the MATLAB Engine . 4-3 The Engine Library . 4-3 GUI-Intensive Applications . 4-5 Examples of Calling Engine Functions . 4-6 Calling MATLAB From a C Application . 4-6 iii Calling MATLAB From a Fortran Application . 4-11 Attaching to an Existing MATLAB Session . 4-15 Compiling and Linking Engine Programs . 4-17 Masking Floating-Point Exceptions . 4-17 Compiling and Linking on UNIX . 4-18 Compiling and Linking on Windows . 4-19 Calling Java from MATLAB 5 Using Java from MATLAB: An Overview . 5-3 Java Interface Is Integral to MATLAB . 5-3 Benefits of the MATLAB Java Interface . 5-3 Who Should Use the MATLAB Java Interface . 5-3 To Learn More About Java Programming . 5-3 Platform Support for the Java Virtual Machine . 5-4 Bringing Java Classes into MATLAB . 5-5 Sources of Java Classes . 5-5 Defining New Java Classes . 5-5 Making Java Classes Available to MATLAB . 5-6 Loading Java Class Definitions . 5-7 Simplifying Java Class Names . 5-8 Creating and Using Java Objects . 5-10 Constructing Java Objects . 5-10 Concatenating Java Objects . 5-12 Saving and Loading Java Objects to MAT-Files . 5-14 Finding the Public Data Fields of an Object . 5-15 Accessing Private and Public Data . 5-16 Determining the Class of an Object . 5-17 Invoking Methods on Java Objects . 5-19 Using Java and MATLAB Calling Syntax . ..