Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Builder EX   

Calling Compiled MATLAB Functions from Microsoft Excel

In order to call compiled MATLAB functions from within a Microsoft Excel spreadsheet, perform the following from the Development and Deployment machines, as specified.

Perform the following steps on the Development machine:

  1. Copy the following files to a work folder on your computer from the Examples folder (matlabroot\toolbox\matlabxl\Examples):

    • doubleit.m

    • incrementit.m

    • powerit.m

  2. From the MATLAB Command Prompt, enter mbuild -setup and select a Visual C++® compiler.

      Note   This procedure was tested using Microsoft Visual C++ 8.0.

  3. Start the Deployment Tool by entering deploytool at the MATLAB Command Prompt.

  4. Use the following information as you work through this example using the instructions in Building Your Component:

    Project Namemyexcelfunctions
    Class Namemyexcelfunctionsclass
    File to compiledoubleit.mincrementit.mpowerit.m

  5. Package your component by following the instructions in Packaging Your Component (Optional).

Perform the following steps on the Deployment machine:

  1. Copy myexcelfunctions_pkg.exe to the deployment machine(s). Copy the file to a standard place for use with Microsoft Excel, such as Office_Installation_folder\Library\MATLAB where Office_Installation_folder is a folder such as C:\Program Files\Microsoft Office\OFFICE11.

  2. Run myexcelfunctions_pkg.exe to extract the archive and register myexcelfunctions_1_0.dll. If you have also included MCRInstaller.exe, follow the prompts to install the MATLAB Compiler Runtime.

  3. Start Microsoft Excel. The spreadsheet Book1 should be open by default.

  4. In Excel, select Tools > Macro > Visual Basic Editor. The Microsoft Visual Basic Editor starts.

  5. In the Microsoft Visual Basic Editor, select File > Import File.

  6. Browse to myexcelfunctions.bas, which was extracted from myexcelfunctions_pkg.exe and click Open. In the Project Explorer, Module1 appears under the Modules node beneath VBAProject (Book1).

  7. In the Microsoft Visual Basic Editor, select View > Microsoft Excel. You can now use the doubleit, incrementit, and powerit functions in your Book1 spreadsheet.

  8. Test the functions, by doing the following:

    1. Enter =doubleit(2.5) in cell A1.

    2. Enter =incrementit(11,17) in cell A2.

    3. Enter =powerit(7,2) in cell A3.

    You should see values 5, 28, and 43 in cells A1, A2, and A3 respectively.

  9. To use the doubleit, powerit, and incrementit functions in all your new Microsoft Excel spreadsheets, do the following:

    1. Select File > Save As.

    2. Change the Save as type option to .xlt (Template).

    3. Browse to the Office_Installation_folder\XLSTART folder.

    4. Save the file as Office_Installation_folder\XLSTART\Book.xlt.

      Note   Your Microsoft Excel Macro Security level must be set at Medium or Low to save this template.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS