Statistics
291
Views
73
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2025-08-30

No description

Tags
No tags
Language: 中文
File Format: PDF
File Size: 2.8 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

MATLAB® Compiler™ MATLAB® Compiler™ Excel® Add-In User's Guide R2021b
How to Contact MathWorks Latest news: www.mathworks.com Sales and services: www.mathworks.com/sales_and_services User community: www.mathworks.com/matlabcentral Technical support: www.mathworks.com/support/contact_us Phone: 508-647-7000 The MathWorks, Inc. 1 Apple Hill Drive Natick, MA 01760-2098 MATLAB® Compiler™ Excel® Add-In User's Guide © COPYRIGHT 2015–2021 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 and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders. Patents MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information. Revision History March 2015 Online only New for Version 6.0 (Release 2015a) September 2015 Online only Revised for Version 6.1 (Release 2015b) October 2015 Online only Rereleased for Version 6.0.1 (Release 2015aSP1) March 2016 Online only Revised for Version 6.2 (Release 2016a) September 2016 Online Only Revised for Version 6.3 (Release 2016b) March 2017 Online only Revised for Version 6.4 (Release R2017a) September 2017 Online only Revised for Version 6.5 (Release R2017b) March 2018 Online only Revised for Version 6.6 (Release R2018a) September 2018 Online only Revised for Version 7.0 (Release R2018b) March 2019 Online only Revised for Version 7.0.1 (Release R2019a) September 2019 Online only Revised for Version 7.1 (Release R2019b) March 2020 Online only Revised for Version 8.0 (Release R2020a) September 2020 Online only Revised for Version 8.1 (Release R2020b) March 2021 Online only Revised for Version 8.2 (Release R2021a) September 2021 Online only Revised for Version 8.3 (Release R2021b)
Create Excel Add-Ins 1 Create Excel Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Getting Started 2 How MATLAB Compiler Excel Integration Works . . . . . . . . . . . . . . . . . . . . 2-2 MATLAB Compiler Excel Add-In Limitations . . . . . . . . . . . . . . . . . . . . . . . 2-2 Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 MATLAB Compiler for Microsoft Excel Add-In Prerequisites . . . . . . . . . . 2-4 Products, Compilers, and IDE Installation . . . . . . . . . . . . . . . . . . . . . . . . . 2-4 Macro Execution Security Levels in Microsoft Excel . . . . . . . . . . . . . . . . . 2-4 Deployment Target Architectures and Compatibility . . . . . . . . . . . . . . . . . 2-4 Dependency and Non-Compilable Code Considerations . . . . . . . . . . . . . . . 2-5 Your Role in the Deployment Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 Choose Function Deployment Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 Is Your Function Ready for Deployment? . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 Other Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 Create Excel Add-In from MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8 Create Function in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8 Create Excel Add-In Using Library Compiler App . . . . . . . . . . . . . . . . . . . 2-8 Package the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10 Create Excel Add-in Using compiler.build.excelAddIn . . . . . . . . . . . . . . . 2-11 Test the Add-In in Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12 Distribute Add-Ins and Integrate Into Microsoft Excel . . . . . . . . . . . . . . 2-14 Integration Process Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14 Key Tasks for Microsoft Excel End User . . . . . . . . . . . . . . . . . . . . . . . . . 2-14 Files To Distribute To End Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14 Add-In and COM Component Registration . . . . . . . . . . . . . . . . . . . . . . . . 2-14 Add-In Installation and Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15 iii Contents
Customizing a Compiler Project 3 Customize an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 Customize the Installer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 Determine Data Type of Command-Line Input (For Packaging Standalone Applications Only) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4 Manage Required Files in Compiler Project . . . . . . . . . . . . . . . . . . . . . . . 3-4 Sample Driver File Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-5 Specify Files to Install with Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6 Additional Runtime Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7 Manage Support Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-9 Using a Compiler App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-9 Using the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-9 The Function Wizard 4 Execute Functions and Create Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 What Can the Function Wizard Do for Me? . . . . . . . . . . . . . . . . . . . . . . . . 4-3 Installation of the Function Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3 Function Wizard Start-Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4 Workflow Selection for MATLAB Functions Ready for Deployment . . . . . . 4-5 Defining Functions Ready to Execute . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5 Function Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13 Macro Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13 Macro Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13 Microsoft Visual Basic Code Access (Optional Advanced Task) . . . . . . . . 4-14 For More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15 End-to-End Deployment of MATLAB Function . . . . . . . . . . . . . . . . . . . . . 4-16 What Can the Function Wizard Do for Me? . . . . . . . . . . . . . . . . . . . . . . . 4-17 Example File Copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18 mymagic Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18 Installation of the Function Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-19 Function Wizard Start-Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-20 Workflow Selection for Prototyping and Debugging MATLAB Functions . 4-21 New MATLAB Function Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-23 MATLAB Function Prototyping and Debugging . . . . . . . . . . . . . . . . . . . . 4-32 Function Execution from MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-32 Microsoft Excel Add-In and Macro Creation Using the Function Wizard . 4-33 Function Execution from the Deployed Component . . . . . . . . . . . . . . . . . 4-34 Macro Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-35 Microsoft Excel Add-In and Macro Packaging using the Function Wizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-35 Microsoft Visual Basic Code Access (Optional Advanced Task) . . . . . . . . 4-36 For More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-37 iv Contents
MATLAB Code Deployment 5 How Does MATLAB Deploy Functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 Dependency Analysis Using MATLAB Compiler . . . . . . . . . . . . . . . . . . . . . 5-3 Function Dependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3 Data File Dependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3 Exclude Files From Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4 MEX-Files, DLLs, or Shared Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5 Deployable Archive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6 Additional Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7 Write Deployable MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9 Packaged Applications Do Not Process MATLAB Files at Run Time . . . . . . 5-9 Do Not Rely on Changing Directory or Path to Control the Execution of MATLAB Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10 Use isdeployed Functions To Execute Deployment-Specific Code Paths . . 5-10 Gradually Refactor Applications That Depend on Noncompilable Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10 Do Not Create or Use Nonconstant Static State Variables . . . . . . . . . . . . 5-10 Get Proper Licenses for Toolbox Functionality You Want to Deploy . . . . . 5-11 Calling Shared Libraries in Deployed Applications . . . . . . . . . . . . . . . . . 5-12 MATLAB Data Files in Compiled Applications . . . . . . . . . . . . . . . . . . . . . 5-13 Explicitly Including MATLAB Data files Using the %#function Pragma . . 5-13 Load and Save Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-13 Microsoft Excel Add-In Creation, Function Execution, and Deployment 6 The Library Compiler and the Command Line Interface . . . . . . . . . . . . . . 6-2 Using Graphical Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 Using Command Line Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 Create Macros from MATLAB Functions with Multiple Arguments . . . . . 6-3 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3 Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3 Inspect the Microsoft Visual Basic Code (Optional) . . . . . . . . . . . . . . . . . . 6-7 Execute Add-In and Graphical Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8 Execute an Add-In to Validate Nongraphical Function Output . . . . . . . . . . 6-8 Execute a Graphical Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8 Create Dialog Box and Error Message Macros . . . . . . . . . . . . . . . . . . . . 6-10 v
Microsoft Excel Add-In Integration 7 Integrate Components Using Visual Basic Application . . . . . . . . . . . . . . . 7-2 When to Use Formula Functions or Subroutines . . . . . . . . . . . . . . . . . . . . 7-2 Initialize MATLAB Compiler Libraries with Microsoft Excel . . . . . . . . . . . . 7-2 Create an Instance of a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-3 Call the Methods of a Class Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4 Program with Variable Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-6 Modify Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-7 Handle Errors During a Method Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-10 Build and Integrate Spectral Analysis Functions . . . . . . . . . . . . . . . . . . . 7-11 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11 Build Excel Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11 Implement VBA code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-12 Test the Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-18 For More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-21 Distribution to End Users 8 Assign Multiple MATLAB Functions to Excel Class . . . . . . . . . . . . . . . . . . 8-2 Create Add-In In MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2 Deploy Add-In to Microsoft Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2 Improve Data Access Using the MATLAB Runtime User Data Interface and COM Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-4 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-4 MATLAB Runtime Component Cache and Deployable Archive Embedding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-8 Overriding Default Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-9 For More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-9 MATLAB Runtime Options for Excel Add-Ins . . . . . . . . . . . . . . . . . . . . . . 8-10 What MATLAB Runtime Options are Supported by MATLAB Compiler? . . 8-10 How Do I Specify MATLAB Runtime Options? . . . . . . . . . . . . . . . . . . . . . 8-10 For More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-11 vi Contents
Functions 9 Utility Library for Microsoft COM Components 10 Reference Utility Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2 Class MWUtil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-3 Sub MWInitApplication(pApp As Object) . . . . . . . . . . . . . . . . . . . . . . . . . 10-3 Sub MWInitApplicationWithMCROptions(pApp As Object, [mcrOptionList]) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-4 Function IsMCRJVMEnabled() As Boolean . . . . . . . . . . . . . . . . . . . . . . . 10-5 Function IsMCRInitialized() As Boolean . . . . . . . . . . . . . . . . . . . . . . . . . 10-5 Sub MWPack(pVarArg, [Var0], [Var1], ... ,[Var31]) . . . . . . . . . . . . . . . . . . 10-5 Sub MWUnpack(VarArg, [nStartAt As Long], [bAutoResize As Boolean = False], [pVar0], [pVar1], ..., [pVar31]) . . . . . . . . . . . . . . . . . . . . . . . . . 10-7 Sub MWDate2VariantDate(pVar) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-8 Class MWFlags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-10 Property ArrayFormatFlags As MWArrayFormatFlags . . . . . . . . . . . . . . 10-10 Property DataConversionFlags As MWDataConversionFlags . . . . . . . . . 10-12 Sub Clone(ppFlags As MWFlags) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-13 Class MWStruct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-15 Sub Initialize([varDims], [varFieldNames]) . . . . . . . . . . . . . . . . . . . . . . 10-15 Property Item([i0], [i1], ..., [i31]) As MWField . . . . . . . . . . . . . . . . . . . . 10-16 Property NumberOfFields As Long . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-17 Property NumberOfDims As Long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-18 Property Dims As Variant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-18 Property FieldNames As Variant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-18 Sub Clone(ppStruct As MWStruct) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-18 Class MWField . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20 Property Name As String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20 Property Value As Variant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20 Property MWFlags As MWFlags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20 Sub Clone(ppField As MWField) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20 Class MWComplex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-21 Property Real As Variant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-21 Property Imag As Variant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-21 Property MWFlags As MWFlags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-21 Sub Clone(ppComplex As MWComplex) . . . . . . . . . . . . . . . . . . . . . . . . 10-22 Class MWSparse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-23 Property NumRows As Long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-23 Property NumColumns As Long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-23 Property RowIndex As Variant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-23 Property ColumnIndex As Variant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-23 Property Array As Variant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-23 vii
Property MWFlags As MWFlags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-23 Sub Clone(ppSparse As MWSparse) . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-23 Class MWArg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-26 Property Value As Variant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-26 Property MWFlags As MWFlags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-26 Sub Clone(ppArg As MWArg) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-26 Enum mwArrayFormat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-27 Enum mwDataType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-28 Enum mwDateFormat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-29 Apps 11 Data Conversion A Data Conversion Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-2 Array Formatting Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-10 Data Conversion Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-12 CoerceNumericToType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-12 InputDateFormat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-13 OutputAsDate As Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-13 DateBias As Long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-13 Troubleshooting B Errors and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-2 Excel Add-Ins Errors and Suggested Solutions . . . . . . . . . . . . . . . . . . . . . B-3 Required Locations to Develop and Use Components . . . . . . . . . . . . . . . . B-5 Microsoft Excel Errors and Suggested Solutions . . . . . . . . . . . . . . . . . . . B-5 Function Wizard Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-6 Deployment Product Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-8 viii Contents
Create Excel Add-Ins 1
Create Excel Add-In 1 Type libraryCompiler at the MATLAB command line to open the Library Compiler app. 2 In the Library Compiler app project window, specify the files of the MATLAB application that you want to deploy. a In the Exported Functions section of the toolstrip, click . b In the Add Files window, browse to the example folder, and select the function you want to package. Click Open. The function is added to the list of exported function files. Repeat this step to package multiple files in the same application. 3 In the Packaging Options section of the toolstrip, decide whether to include the MATLAB Runtime installer in the generated application by selecting one of the options: • Runtime downloaded from web — Generate an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application. You can specify the file name of the installer. • Runtime included in package — Generate an application that includes the MATLAB Runtime installer. You can specify the file name of the installer. Note The first time you select this option, you are prompted to download the MATLAB Runtime installer. 4 Verify that the MATLAB function you are packing into an Excel add-in is mapped onto a class. You can change the name from the default name Class 1 to something more meaningful. 5 Customize the packaged application and its appearance: • Library information — Editable information about the deployed application. You can also customize the standalone applications appearance by changing the application icon and splash screen. The generated installer uses this information to populate the installed application metadata. See “Customize the Installer”. • Additional installer options — Edit the default installation path for the generated installer and selecting custom logo. See “Change the Installation Path” . • Files required for your library to run — Additional files required by the generated application to run. These files are included in the generated application installer. See “Manage Required Files in Compiler Project”. • Files installed for your end user — Files that are installed with your application. See “Specify Files to Install with Application”. • Additional runtime settings — Platform-specific options for controlling the generated executable. See “Additional Runtime Settings”. 6 When you are finished selecting your packaging options, save your Library Compiler project and generate the packaged application. 1 Click Package. In the Save Project dialog box, specify the location to save the project. 2 In the Package dialog box, verify that Open output folder when process completes is selected. 1 Create Excel Add-Ins 1-2
When the packaging process is complete, examine the generated output in the target folder. • Three folders are generated: for_redistribution, for_redistribution_files_only, and for_testing. For more information about the files generated in these folders, see “Files Generated After Packaging MATLAB Functions”. • The log file PackagingLog.html contains packaging results. See Also More About • “Create Excel Add-In from MATLAB” on page 2-8 Create Excel Add-In 1-3
(This page has no text content)
Getting Started • “How MATLAB Compiler Excel Integration Works” on page 2-2 • “MATLAB Compiler for Microsoft Excel Add-In Prerequisites” on page 2-4 • “Choose Function Deployment Workflow” on page 2-7 • “Create Excel Add-In from MATLAB” on page 2-8 • “Distribute Add-Ins and Integrate Into Microsoft Excel” on page 2-14 2
How MATLAB Compiler Excel Integration Works With MATLAB Compiler, you create deployable add-ins from MATLAB code that run in Microsoft® Excel applications. Use the Library Compiler app, compiler.build.excelAddIn, or mcc to convert MATLAB functions to methods of a class that you define. From this class, the compiler creates the Excel add-in. MATLAB Compiler generates two primary artifacts from your MATLAB code: a COM component (.dll file) and a Visual Basic® for Applications (VBA) module file (.bas file). The compiler integrates the COM wrapper with the compiler-generated VBA code to create a Microsoft Excel add-in (.xla file), saving you considerable development resources and time. COM is an acronym for Component Object Model, which is a Microsoft binary standard for object interoperability. COM components use a common integration architecture that provides a consistent model across multiple applications. All Microsoft Office applications support COM add-ins. Each COM object exposes a class to the Visual Basic programming environment. The class contains a set of functions called methods. These methods correspond to the original MATLAB functions included in the project. MATLAB Compiler generated COM components contain a single class. This class provides the interface to the MATLAB functions that you add to the class at build time. The COM component provides a set of methods that wrap the MATLAB code and a DLL file. The MATLAB Compiler generates supporting files. Include these supporting files when you package and distribute an application. Include MATLAB Runtime to access an entire library of MATLAB functions without needing a licensed copy of MATLAB. For information about how MATLAB Compiler works, see “How Does MATLAB Deploy Functions?” on page 5-2. To verify your MATLAB code or function can be deployed successfully, see “Write Deployable MATLAB Code”. MATLAB Compiler Excel Add-In Limitations Generated add-ins support the following function outputs: • No outputs • Figure (graphical) output • Scalar output • Multidimensional matrix output MATLAB objects are not supported as inputs or outputs for compiled or deployed functions with MATLAB Compiler for Excel add-ins. Next Steps MATLAB Compiler includes several examples in addition to the magic square example. You can find these examples in matlabroot\toolbox\matlabxl\examples\. The following table identifies examples by folder: 2 Getting Started 2-2
For Example Files Relating To... Find Example Code in Folder... For Example Documentation See... Magic Square Example xlmagic “Create Excel Add-In from MATLAB” on page 2-8 Variable-Length Argument Example xlmulti “Create Macros from MATLAB Functions with Multiple Arguments” on page 6-3 Calling Compiled MATLAB Functions from Microsoft Excel xlbasic “Assign Multiple MATLAB Functions to Excel Class” on page 8-2 Spectral Analysis Example xlspectral “Build and Integrate Spectral Analysis Functions” on page 7- 11 The following topics detail some of the more common tasks you perform as you continue to develop your applications. To: See: Try more examples using the MATLAB Compiler MATLAB Central. Set the Search field to File Exchange and search for one or more of the following: • InterpExcelDemo • MatrixMathExcelDemo • ExcelCurveFit Learn how to write MATLAB code that is optimized for deployment “Write Deployable MATLAB Code” Work with functions having graphical output “Execute Add-In and Graphical Function” on page 6-8 Work with functions having variable inputs and output “Create Macros from MATLAB Functions with Multiple Arguments” on page 6-3 Create displayable dialog boxes and error messages “Create Dialog Box and Error Message Macros” on page 6-10 Troubleshoot common error messages “Errors and Solutions” on page B-2 Integrate your application into your enterprise environment by enhancing your application's generated Visual Basic code “Integrate Components Using Visual Basic Application” on page 7-2 See Also Related Examples • “Distribute Add-Ins and Integrate Into Microsoft Excel” on page 2-14 • “How Does MATLAB Deploy Functions?” on page 5-2 • “Write Deployable MATLAB Code” How MATLAB Compiler Excel Integration Works 2-3
MATLAB Compiler for Microsoft Excel Add-In Prerequisites In this section... “Products, Compilers, and IDE Installation” on page 2-4 “Macro Execution Security Levels in Microsoft Excel” on page 2-4 “Deployment Target Architectures and Compatibility” on page 2-4 “Dependency and Non-Compilable Code Considerations” on page 2-5 “Your Role in the Deployment Process” on page 2-5 Products, Compilers, and IDE Installation To generate Microsoft Excel add-ins using MATLAB, you must have the following installed: • MATLAB • MATLAB Compiler • A supported C or C++ compiler Macro Execution Security Levels in Microsoft Excel To create macros and generate add-ins with MATLAB Compiler, you must adjust the security settings accordingly in Microsoft Excel. Failure to do so may result in add-ins not being generated or warning messages sent to MATLAB Compiler Depending on what version of Microsoft Excel you are using, do one of the following: • For Microsoft Excel 2010 or newer: 1 Click File > Options > Trust Center > Trust Center Settings > Macro Settings. 2 In Developer Macro Settings, select Trust access to the VBA project object model. • For Microsoft Excel 2007: 1 Click the 2007 Excel ribbon button. 2 Click Excel Options > Trust Center > Trust Center Settings > Macro Settings. 3 In Developer Macro Settings, select Trust access to the VBA project object model. • For Microsoft Excel 2003: 1 Click Tools > Macro > Security. 2 For Security Level, select Medium. Deployment Target Architectures and Compatibility Before you deploy a component with MATLAB Compiler, consider if your target machines are 32-bit or 64-bit. 2 Getting Started 2-4
Applications developed on one architecture must be compatible with the architecture on the system where they are deployed. For example, if you have a 64-bit system, you usually install a 64-bit version of MATLAB (and most other applications) by default. Running functions you have developed with a 64-bit version of MATLAB requires a 64-bit version of Microsoft Excel. Migration Considerations for 32-bit and 64-bit Microsoft Excel Add-ins created with MATLAB Compiler are compatible with both 32–bit and 64–bit versions of Microsoft Excel. MATLAB Compiler itself is in 64–bit only. Dependency and Non-Compilable Code Considerations Before you deploy your code, examine the code for dependencies on functions that may not be compatible with MATLAB Compiler. For more detailed information about dependency analysis (depfun) and how MATLAB Compiler evaluates MATLAB code prior to compilation, see “Write Deployable MATLAB Code”. Your Role in the Deployment Process The table below describes the different roles, or jobs, that MATLAB Compiler users typically perform. It also describes tasks they would most likely perform when running the examples in this documentation. You may occupy one or more of the following roles. MATLAB Compiler for Microsoft Excel Add-In Prerequisites 2-5
Application Deployment Roles, Goals, and Tasks Role Knowledge Base Responsibilities MATLAB programmer • Understand the end-user business requirements and the mathematical models that support them. • MATLAB expert • No IT experience • Build a Microsoft Excel add- in with MATLAB tools. • Package the component for distribution to customers. • Pass the package to the Microsoft Excel developer for further integration into the end-user environment. . Microsoft Excel developer • Some or no MATLAB experience. • Microsoft Excel expert. • Proficient writing VB/VBA code. • Roll out the packaged component and integrate it into the end-user environment. • Write VB/VBA code to complement or augment the Excel add-in built by the MATLAB programmer. Add and modify code as needed. • Uses Function Wizard to customize the add-in and create executable macros. • Verify that the final application executes reliably in the end-user environment. 2 Getting Started 2-6
Choose Function Deployment Workflow In this section... “Is Your Function Ready for Deployment?” on page 2-7 “Other Examples” on page 2-7 Is Your Function Ready for Deployment? If These Statements are True... See... • I have a MATLAB function that conforms to guidelines in “Write Deployable MATLAB Code”. • I want to create a Microsoft Excel compatible add-in from my existing MATLAB function. • I have tested and debugged my MATLAB function with MATLAB. See “Create Excel Add-In from MATLAB” on page 2- 8 to build your add-in and “Distribute Add-Ins and Integrate Into Microsoft Excel” on page 2-14 to execute the newly built add- in and create macros and associated GUIs with the Function Wizard and Microsoft Excel. • I have not yet developed a MATLAB function for deployment as an add-in, or I am in the process of developing it. • I have not tested my MATLAB function thoroughly with MATLAB. See “End-to-End Deployment of MATLAB Function” on page 4-16 for an end-to-end example of creating, debugging, building, and packaging MATLAB function from scratch using the Function Wizard. Other Examples For other types of examples, see the following: • “Execute Add-In and Graphical Function” on page 6-8 • “Create Macros from MATLAB Functions with Multiple Arguments” on page 6-3 • MATLAB Central. Set the Search field to File Exchange and search for one or more of the following: • InterpExcelDemo • MatrixMathExcelDemo • ExcelCurveFit Choose Function Deployment Workflow 2-7
Create Excel Add-In from MATLAB Supported Platform: Windows® only. This example shows how to use MATLAB Compiler to generate a Microsoft Excel add-in containing a custom function for use within Excel. The custom function mymagic returns an n-by-n matrix filled with positive integers with equal row and column sums. The target system does not require a licensed copy of MATLAB to run the add-in. Prerequisites • Verify that you have met all of the MATLAB Compiler Excel target requirements. For details, see “MATLAB Compiler for Microsoft Excel Add-In Prerequisites” on page 2-4. • Verify that you have Microsoft Excel installed. • End users must have an installation of MATLAB Runtime to run the add-in. For details, see “Install and Configure MATLAB Runtime” (MATLAB Compiler SDK). For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime. Create Function in MATLAB In MATLAB, locate the MATLAB code that you want to deploy as an Excel add-in. For this example, compile using the file mymagic.m located in matlabroot\toolbox\matlabxl \examples\xlmagic. function y = mymagic(x) y = magic(x) At the MATLAB command prompt, enter mymagic(5). The output is a 5-by-5 square. 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 Create Excel Add-In Using Library Compiler App Package the function into an Excel add-in using the Library Compiler app. Alternatively, if you want to create an Excel add-in from the MATLAB command window using a programmatic approach, see “Create Excel Add-in Using compiler.build.excelAddIn” on page 2-11. 1 On the MATLAB Apps tab, on the far right of the Apps section, click the arrow. In Application Deployment, click Library Compiler. In the MATLAB Compiler project window, click Excel Add-in. 2 Getting Started 2-8