Create Excel Add-In from MATLAB
Supported Platform: Microsoft® 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 function
mymagic returns an n-by-n magic square
matrix that has equal row and column sums. The target system does not require a licensed
copy of MATLAB to run the add-in.
Before you begin, verify that you have met all of the Excel target requirements for MATLAB Compiler. For details, see Excel Target Requirements and Limitations for MATLAB Compiler.
To generate the Visual Basic® files, enable Trust access to the VBA project object
model in Excel. If you do not do this, you can manually create the add-in by importing
.bas file into Excel.
Create Function in MATLAB
In MATLAB, locate the MATLAB code that you want to deploy as an Excel add-in.
For this example, compile the function
mymagic.m located in
function y = mymagic(x) y = magic(x)
At the MATLAB command prompt, enter
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.
To open the Library Compiler app, type
libraryCompilerat the MATLAB prompt.
Alternatively, 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, select Excel Add-in in the TYPE section.
In the Library Compiler app project window, specify the files of the MATLAB application that you want to deploy.
In the Exported Functions section of the toolstrip, click .
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.
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.
The first time you select this option, you are prompted to download the MATLAB Runtime installer.
Next, define the name of your add-in and verify the class mapping for the
.mfile that you are building into your application.
The Library Name field is automatically populated with
mymagicas the name of the assembly. Rename it as
xlmagic. The same name is followed through in the implementation of the add-in.
Verify that the function defined in
mymagic.mis mapped into
xlmagicclass. Double-click on the class to change the class name.
Customize the packaged application and its appearance:
Library information — Editable information about the deployed application. 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.
Additional runtime settings — Platform-specific options for controlling the generated executable. See Additional Runtime Settings.
Package the Application
When you are finished selecting your packaging options, save your Library Compiler project and generate the packaged application.
In the Save Project dialog box, specify the location to save the project.
In the Package dialog box, verify that Open output folder when process completes is selected.
When the packaging process is complete, examine the generated output in the target folder.
Three folders are generated:
For more information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.
The log file
PackagingLog.htmlcontains packaging results.
Create Excel Add-in Using
As an alternative to the Library Compiler app, you can create an Excel add-in using a programmatic approach. If you have already created an add-in using the Library Compiler, see Install Add-In in Excel.
In MATLAB, locate the MATLAB code that you want to deploy as a standalone application. For this example, compile using the file
appFile = fullfile(matlabroot,'toolbox','matlabxl','examples','xlmagic','mymagic.m');
Build the Excel add-in using the
compiler.build.excelAddInfunction. Use name-value arguments to set the library and class names, and enable the generation of Visual Basic files.
buildResults = compiler.build.excelAddIn(appFile, ... 'AddInName','xlmagic', ... 'ClassName','xlmagicclass', ... 'GenerateVisualBasicFile','on');
You can specify additional options in the
compiler.buildcommand by using name-value arguments. For details, see
buildResultscontains information on the build type, generated files, included support packages, and build options.
The function generates the following files within a folder named
mymagicexcelAddInin your current working directory:
The generated add-in does not include MATLAB Runtime or an installer. To create an installer using the
Install Add-In in Excel
Open Microsoft Excel.
Click the File tab, click Options, and then click the Add-Ins category.
In the Manage box, click Excel Add-ins, and then click Go. The Add-Ins dialog box appears.
Click Browse and locate the add-in
You are prompted to copy
Addinsfolder associated with your user name. You can choose to copy the add-in or run it directly. For this example, select, YES. The add-in is copied and added to your workbook.
Click OK to close the Add-Ins dialog box
Select a grid of 3-by-3 cells in the Excel workbook.
Enter the following custom function in the formula bar:As you type
myin the formula bar,
mymagicappears as a custom function in Excel.
Press Ctrl+Shift+Enter on the keyboard.
The selected cells display the following output:
8 1 6 3 5 7 4 9 2
For additional examples, open the file
. This spreadsheet contains three custom VBA macros that demonstrate various ways of using the compiled MATLAB function.
To distribute your add-in to end users, see Distribute Add-Ins and Integrate into Microsoft Excel.