Main Content

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.

Enable Access to VBA Project Model

To generate the .xla file, first 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 the .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.

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 compiler.build.excelAddIn

Build the Excel add-in using the compiler.build.excelAddIn function. Use name-value arguments to set the library and class names and enable the generation of Visual Basic files.

buildResults = compiler.build.excelAddIn('mymagic.m',...
'AddInName','xlmagic',...
'ClassName','xlmagicclass',...
'GenerateVisualBasicFile','on');

You can specify additional options in the command by using name-value arguments. For details, see compiler.build.excelAddIn.

The compiler.build.Results object buildResults contains information on the build type, generated files, included support packages, and build options.

Distribute the following files generated in the mymagicexcelAddIn folder to the target machine:

  • xlmagic.bas — VBA module file that can be imported into a VBA project.

  • xlmagic.xla — Excel add-in that can be added directly to Excel. You do not need both .bas file and .xla file; one of them is sufficient.

  • xlmagic_1_0.dll — Generated dll that needs to be registered using mwregsvr.exe or regsvr32.exe.

The generated add-in does not include MATLAB Runtime or an installer. To create an installer using the buildResults object, see compiler.package.installer.

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 xlmagic.xla.

You are prompted to copy xlmagic.xla to the Addins folder 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.

Test Add-In

Select a grid of 3-by-3 cells in the Excel workbook.

Enter the following custom function in the formula bar:

=mymagic(3)

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 xlmagic.xls located in matlabroot\toolbox\matlabxl\examples\xlmagic. This spreadsheet contains three custom VBA macros that demonstrate various ways of using the compiled MATLAB function.

Distribute Add-In

To distribute your add-in to end users, see Distribute Add-Ins and Integrate into Microsoft Excel.

See Also

|

Related Topics