Supported Platform: Windows® only.
This example shows how to create a Microsoft®
Excel® add-in containing a custom function for use within Excel. The custom function called
mymagic returns an
n-by-n matrix filled with positive integers with equal row and column sums.
mymagic is written in MATLAB® and packaged as an Excel add-in using the Library Compiler app in MATLAB
The add-in can be installed on a machine running Excel using the installer generated by the Library Compiler app. Once installed, you add the add-in to your Excel workbook.
The machine where the add-in is installed does not require an installation of MATLAB. However, it does require an installation of MATLAB Runtime. When installing the add-in on a machine, the installer generated by the Library Compiler app will automatically install MATLAB Runtime.
Create a MATLAB function named
mymagic that returns an n-by-n
matrix filled with positive integers with equal row and column sums. Save the
function in a file named
function y = mymagic(x) y = magic(x)
Test the function at the MATLAB command line.
m = mymagic(5)
m = 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
libraryCompiler at the MATLAB command line to open the Library Compiler app.
In the TYPE section of the toolstrip, select Excel add-in as your target type.
In the EXPORTED FUNCTIONS section of the
toolstrip, click to add the file
mymagic.m to the project.
In the Library information section of
the app, the library name is automatically updated to
mymagic, the class name is updated to
Class1, and the method name to
[y] = mymagic(x).
In the Files installed for your end user section of the app, the files installed when the add-in is installed on a machine are automatically displayed. These files include:
Click Package to package the MATLAB function as an add-in.
In the Save Project dialog box that opens, specify a project name and a location where you want to save the project. Library Compiler saves your project and opens a Package dialog box.
When the packaging process is complete, three folders are
generated in the target folder location:
for_redistribution folder contains
the installer file
that installs the add-in and the MATLAB Runtime. The
contains the files that are installed on an end user's
machine. These are the same files that are installed by the
installer. It contains the following files:
For more information about the folders, see Files Generated After Packaging MATLAB Functions.
If you have already created an Excel add-in using the Library Compiler app, you can skip this section. However, if you want to know how to create an add-in from the MATLAB command window using a programmatic approach, follow these instructions.
Build the Excel add-in using the
compiler.build.excelAddIn function and the
mymagic.m file that you wrote earlier. Use
name-value arguments to generate the BAS and XLA files.
buildResults = compiler.build.standaloneApplication('mymagic.m',... 'GenerateVisualBasicFile','on');
buildResults contains information on the build
type, generated files, and build options.
The function generates the following files within a folder named
mymagicexcelAddIn in your current working
Additional options can be specified by using one or more
comma-separated pairs of name-value arguments in the
'AddInName' — Name of the generated
'AddInVersion' — System level version of
the generated add-in.
'AdditionalFiles' — Paths to additional
files to include in the add-in.
'AutoDetectDataFiles' — Flag to
automatically include data files.
'ClassName' — Name of the class.
'DebugBuild' — Flag to enable debug
'EmbedArchive' — Flag to embed the
standalone archive in the generated executable.
'GenerateVisualBasicFile' — Flag to
generate a Visual Basic® file (
.bas) and an Excel add-in file (
'OutputDirectory' — Path to the output
directory that contains generated files.
'Verbose' — Flag to display progress
information indicating compiler output during the build
You may have to enable Trust access to the VBA project object model in Excel for the add-in to work.
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
mymagic.xla in the
You are prompted to copy
mymagic.xla to the
Addins folder associated with your user name. You can choose to copy
the add-in or run it from the
for_redistribution_files_only folder. 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:
myin the formula bar, you see
mymagicshowing up as a custom function in Excel.
Press Ctrl+Shift+Enter on the keyboard.
You see the following output displayed in the cells:
8 1 6 3 5 7 4 9 2