Main Content

CreateExcelAdd-In fromMATLAB

Supported Platform:Microsoft®Windows®only.

This example shows how to useMATLAB®编译器™to generate aMicrosoft Excel®add-in containing a custom function for use within Excel. The functionmymagicreturns 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 forMATLAB Compiler. For details, seeExcel Target Requirements and Limitations for MATLAB Compiler.

Note

To generate the Visual Basic®files, enableTrust access to the VBA project object modelin Excel. If you do not do this, you can manually create the add-in by importing the.basfile into Excel.

Create Function inMATLAB

In MATLAB, locate the MATLAB code that you want to deploy as an Excel add-in.

For this example, compile the functionmymagic.mlocated inmatlabroot\toolbox\matlabxl\examples\xlmagic.

functiony = mymagic(x) y = magic(x)

At the MATLAB command prompt, entermymagic(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

CreateExcelAdd-In Using Library Compiler App

Package the function into an Excel add-in using theLibrary Compilerapp. Alternatively, if you want to create an Excel add-in from the MATLAB command window using a programmatic approach, seeCreate Excel Add-in Using compiler.build.excelAddIn.

  1. To open the Library Compiler app, typelibraryCompilerat the MATLAB prompt.

    Alternatively, on theMATLAB Apps选项卡,最右边的Appssection, click the arrow. InApplication Deployment, clickLibrary Compiler.

  2. In theMATLAB Compilerproject window, selectExcel Add-inin theTYPEsection.

    Library Compiler with Excel Add-in type selected and mymagic.m in the exported function section

  3. In theLibrary Compilerapp project window, specify the files of the MATLAB application that you want to deploy.

    1. In theExported Functionssection of the toolstrip, clickAdd exported function to the project.

    2. In theAdd Fileswindow, browse to the example folder, and select the function you want to package. ClickOpen.

    The function is added to the list of exported function files. Repeat this step to package multiple files in the same application.

  4. In thePackaging Optionssection of the toolstrip, decide whether to include theMATLAB Runtimeinstaller in the generated application by selecting one of the options:

    • Runtime downloaded from web— Generate an installer that downloads theMATLAB Runtimeand 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 theMATLAB Runtimeinstaller. You can specify the file name of the installer.

      Note

      The first time you select this option, you are prompted to download theMATLAB Runtimeinstaller.

  5. Next, define the name of your add-in and verify the class mapping for the.mfile that you are building into your application.

    1. TheLibrary Namefield is automatically populated withmymagicas the name of the assembly. Rename it asxlmagic. The same name is followed through in the implementation of the add-in.

    2. Verify that the function defined inmymagic.mis mapped intoxlmagicclass. Double-click on the class to change the class name.

    Library Compiler Class Name section with class name xlmagicclass for the method mymagic

  6. 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. SeeCustomize the Installer.

    • Additional installer options— Edit the default installation path for the generated installer and selecting custom logo. See改变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. SeeManage Required Files in Compiler Project.

    • Files installed for your end user— Files that are installed with your application.

      SeeSpecify Files to Install with Application.

    • Additional runtime settings— Platform-specific options for controlling the generated executable. SeeAdditional Runtime Settings.

    Library Compiler with project name xlmagic and class name xlmagicclass

Package the Application

When you are finished selecting your packaging options, save yourLibrary Compilerproject and generate the packaged application.

  1. ClickPackage.

    In the Save Project dialog box, specify the location to save the project.

  2. In thePackagedialog box, verify thatOpen output folder when process completesis 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, andfor_testing.

      关于生成的文件的更多信息these folders, seeFiles Generated After Packaging MATLAB Functions.

    • The log filePackagingLog.htmlcontains packaging results.

CreateExcelAdd-in Usingcompiler.build.excelAddIn

As an alternative to theLibrary Compilerapp, you can create an Excel add-in using a programmatic approach. If you have already created an add-in using theLibrary Compiler, seeInstall 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 filemymagic.mlocated inmatlabroot\toolbox\matlabxl\examples\xlmagic.

    appFile = fullfile(matlabroot,'toolbox','matlabxl','examples','xlmagic','mymagic.m');
  2. Build the Excel add-in using thecompiler.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 thecompiler.buildcommand by using name-value arguments. For details, seecompiler.build.excelAddIn.

    Thecompiler.build.ResultsobjectbuildResultscontains information on the build type, generated files, included support packages, and build options.

    The function generates the following files within a folder namedmymagicexcelAddIn在你的current working directory:

    • dlldata.c

    • GettingStarted.html

    • includedSupportPackages.txt

    • xlmagic.def

    • xlmagic.bas

    • xlmagic.rc

    • xlmagic.xla

    • xlmagic_1_0.dll

    • xlmagic_dll.cpp

    • xlmagic_idl.h

    • xlmagic_idl.idl

    • xlmagic_idl.tlb

    • xlmagic_idl_i.c

    • xlmagic_idl_p.c

    • xlmagicClass_com.cpp

    • xlmagicClass_com.hpp

    • mccExcludedFiles.log

    • mwcomtypes.h

    • mwcomtypes_i.c

    • mwcomtypes_p.c

    • readme.txt

    • requiredMCRProducts.txt

    • unresolvedSymbols.txt

    Note

    The generated add-in does not includeMATLAB Runtimeor an installer. To create an installer using thebuildResultsobject, seecompiler.package.installer.

Install Add-In inExcel

  1. OpenMicrosoft Excel.

  2. Click theFiletab, clickOptions, and then click theAdd-Inscategory.

  3. In theManagebox, clickExcel Add-ins, and then clickGo. TheAdd-Insdialog box appears.

  4. ClickBrowseand locate the add-inxlmagic.xla.

  5. You are prompted to copyxlmagic.xlato theAddinsfolder 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. ClickOKto close theAdd-Insdialog box

Test 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 typemyin the formula bar,mymagicappears as a custom function in Excel.

  3. PressCtrl+Shift+Enteron the keyboard.

    The selected cells display the following output:

    8 1 6 3 5 7 4 9 2
  4. For additional examples, open the filexlmagic.xlslocated inmatlabroot\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, seeDistribute Add-Ins and Integrate into Microsoft Excel.

See Also

|

Related Topics