Main Content

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

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.

  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.

    Alternately, you can open the Library Compiler app by entering libraryCompiler at the MATLAB prompt.

  2. In the Library Compiler app project window, specify the files of the MATLAB application that you want to deploy.

    1. In the Exported Functions section of the toolstrip, click Add exported function to the project.

    2. 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 function defined in mymagic.m is mapped into Class1.

  5. 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.

      See Specify Files to Install with 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.

  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.

    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.

Create Excel Add-in Using compiler.build.excelAddIn

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 Test the Add-In in Excel.

  1. In MATLAB, locate the MATLAB code that you want to deploy as a standalone application. For this example, compile using the file mymagic.m located in matlabroot\toolbox\matlabxl\examples\xlmagic.

    appFile = fullfile(matlabroot,'toolbox','matlabxl','examples','xlmagic','mymagic.m');
  2. Build the Excel add-in using the compiler.build.excelAddIn function. Use name-value arguments to generate the BAS and XLA files.

    Note

    To generate the XLA file, enable Trust access to the VBA project object model in Excel.

    buildResults = compiler.build.excelAddIn(appFile, ...
        'GenerateVisualBasicFile','on')

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

    The function generates the following files within a folder named mymagicexcelAddIn in your current working directory:

    • dlldata.c

    • GettingStarted.html

    • includedSupportPackages.txt

    • mymagic.def

    • mymagic.bas

    • mymagic.rc

    • mymagic.xla

    • mymagic_1_0.dll

    • mymagic_dll.cpp

    • mymagic_idl.h

    • mymagic_idl.idl

    • mymagic_idl.tlb

    • mymagic_idl_i.c

    • mymagic_idl_p.c

    • mymagicClass_com.cpp

    • mymagicClass_com.hpp

    • mccExcludedFiles.log

    • mwcomtypes.h

    • mwcomtypes_i.c

    • mwcomtypes_p.c

    • readme.txt

    • requiredMCRProducts.txt

    • unresolvedSymbols.txt

    Note

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

    Specify additional options by using one or more comma-separated pairs of name-value arguments in the compiler.build command.

    • 'AddInName' — Name of the generated add-in.

    • '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 symbols..

    • 'EmbedArchive' — Flag to embed the deployable archive in the generated add-in.

    • 'GenerateVisualBasicFile' — Flag to generate a Visual Basic® file (.bas) and an Excel add-in file (.xla).

    • 'OutputDirectory' — Path to the output directory that contains generated files.

    • 'SupportPackages' — Method to include support packages.

    • 'Verbose' — Flag to display progress information indicating compiler output during the build process.

    For example, you can specify the add-in name and enable verbose output.

    buildResults = compiler.build.excelAddIn(appFile,...
        'GenerateVisualBasicFile','on', ...
        'AddInName','MyMagicExcel','Verbose','On');

Test the Add-In in Excel

Add the Add-In to Excel

  1. Open Microsoft Excel.

  2. Click the File tab, click Options, and then click the Add-Ins category.

  3. In the Manage box, click Excel Add-ins, and then click Go. The Add-Ins dialog box appears.

  4. Click Browse and locate the add-in mymagic.xla.

  5. 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 directly. For this example, select, YES. The add-in is copied and added to your workbook.

  6. Click OK to close the Add-Ins dialog box

Test the Add-In

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

  2. Enter the following custom function in the formula bar:

    =mymagic(3)
    As you type my in the formula bar, mymagic appears as a custom function in Excel.

  3. Press Ctrl+Shift+Enter on the keyboard.

    The selected cells display the following output:

    8	1	6
    3	5	7
    4	9	2

Distribute Add-In

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

See Also

|

Related Topics