Main Content

Distribute Add-Ins and Integrate into Microsoft Excel

To create a Microsoft® Excel® add-in, see Create Excel Add-In from MATLAB.

After you have created your Excel add-in, follow these steps to deploy the add-in on another machine.

  1. Verify that you have received all the files necessary for deployment.

  2. Install MATLAB® Runtime.

  3. Register your add-in DLL using the generated installer, the _install.bat installation script, or mwregsvr.

  4. Register the utility libraries mwcomutil.dll and mwcommgr.dll using mwregsvr.

  5. Install the add-in in Excel.

  6. Call the add-in functions directly in your worksheet.

  7. (Optional) Create macros using the Function Wizard or custom VBA code.

Files for Excel Add-In Deployment

If you create your Excel add-in using the Library Compiler or create an installer using compiler.package.installer, distribute the generated installer to your end user. The installer installs the Excel add-in and MATLAB Runtime and registers the add-in DLL on the target machine. The Library Compiler also generates an installation script named _install.bat that registers the add-in DLL. To register the DLL without using the installer or script, see Register Add-Ins and COM Components below.

If you do not create an installer, distribute the files that the Library Compiler generates in the for_redistribution_files_only folder. For a list of files generated in each folder, see Files Generated After Packaging MATLAB Functions.

Install MATLAB Runtime

MATLAB Runtime contains the libraries needed to use compiled MATLAB code on a target system without a licensed copy of MATLAB. For instructions, see Download and Install MATLAB Runtime.

The MATLAB Runtime installer for Windows® automatically sets the system path and registers the MATLAB Runtime DLLs required for Excel add-ins. To perform these steps manually, see Deploy Applications and MATLAB Runtime on Network Drives.

Register Add-Ins and COM Components

Note

COM components are used in both MATLAB Compiler™ and MATLAB Compiler SDK™, therefore some of the instructions relating to building and packaging COM components and add-ins can be shared between products.

When you create your COM component, it is registered in either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER, based on your log-in privileges.

If you find you need to change your run-time permissions due to security standards imposed by Microsoft or your installation, you can do one of the following before deploying your COM component or add-in:

  • Log on as administrator before running your COM component or add-in

  • Run the following mwregsvr command prior to running your COM component or add-in, as follows:

    mwregsvr [/u] [/s] [/useronly] project_name.dll 
    where:

    • /u allows any user to unregister a COM component or add-in for this server

    • /s runs this command silently, generating no messages. This is helpful for use in silent installations.

    • /useronly allows only the currently logged-in user to run the COM component or add-in on this server

Caution

If your COM component is registered in the USER hive, it will not be visible to Windows Vista® or Windows 7 users running as administrator on systems with UAC (User Access Control) enabled.

If you register a component to the USER hive under Windows 7 or Windows Vista, your COM component may fail to load when running with elevated (administrator) privileges.

If this occurs, do the following to re-register the component to the LOCAL MACHINE hive:

  1. Unregister the component with this command:

    mwregsvr /u /useronly my_dll.dll
    

  2. Reregister the component to the LOCAL MACHINE hive with this command:

    mwregsvr my_dll.dll

Register MATLAB Utility Libraries

To handle data conversion, the VBA code that you generate with MATLAB Compiler references the mwcomutil.dll and mwcommgr.dll utility libraries. To register these libraries:

  1. Open a system command prompt

  2. Navigate to matlabroot\bin\win64, where matlabroot represents the location of MATLAB or MATLAB Runtime that corresponds to the MATLAB release that you used to compile the Excel add-in.

  3. Run the following commands:

    mwregsvr mwcomutil.dll
    mwregsvr mwcommgr.dll

Install Excel Add-Ins

Install the add-in in Microsoft Excel by following the steps for your Excel version.

Excel 2010 or Newer

  1. In Excel, click the File tab.

  2. On the left navigation pane, select Options.

  3. In the Excel Options dialog box, on the left navigation pane, select Add-ins.

  4. In the Manage drop-down list, select Excel Add-ins, and click Go.

  5. In the Add-ins dialog box, click Browse.

  6. Browse to and select your add-in. Click OK.

  7. In the Excel Add-ins dialog box, verify that the entry containing your add-in is selected. Click OK.

Excel 2007

  1. Start Microsoft Excel.

  2. Click the Office button () and select Excel Options.

  3. In the left pane of the Excel Options dialog box, click Add-ins.

  4. In the right pane of the Excel Options dialog box, select Excel Add-ins from the Manage drop-down list.

  5. Click Go.

  6. Click Browse.

  7. Browse to and select your add-in. Click OK.

  8. In the Excel Add-ins dialog box, verify that the entry containing your add-in is selected. Click OK.

Use MATLAB Functions in Excel Spreadsheet

After you have installed the add-in in Excel, invoke the add-in function directly with a method call in the form of an Excel custom function.

Optionally, use an Excel macro to specify input and output cells, execute multiple functions at a time, or handle multiple outputs. Create a macro using the Function Wizard add-in provided with MATLAB Compiler. For additional functionality, create or modify macros using custom VBA code.

Call Method Directly in Spreadsheet

To run the MATLAB function in the Excel spreadsheet, invoke the function with a method call in the target output cells. For example, if you deployed MATLAB code called mymagic.m or a figure called mymagic.fig, invoke that code by entering the following function in a cell in the spreadsheet:

=mymagic()

If your function returns an array, select a range of cells to match the array size.

Tip

If the method call does not evaluate immediately, press Ctrl, Shift, and Enter simultaneously.

Create Excel Macros Using Function Wizard

The Function Wizard provides a control panel for creating macros using one or more compiled MATLAB functions. Use the Function Wizard to control the placement of MATLAB input and output data in Excel worksheets.

For more details, see Install and Use Function Wizard.

Create Excel Macros Using VBA

Use VBA code to create Excel macros with more functionality than the Function Wizard allows. You can use variables, loops, forms, controls, and other programming techniques to create custom Excel programs.

For an example on using VBA code to create a custom graphical Excel application, see Implement User Interface Using Visual Basic Form Controls.

Related Topics