Main Content

Assign Multiple MATLAB Functions to Excel Class

Create Add-In In MATLAB

Note

In order for a function to be called using the Microsoft® Excel® function syntax (=myfunction(input)), the MATLAB® function must return a single scalar output argument.

  1. Copy the xlbasic folder that ships with MATLAB to your work folder:

    copyfile(fullfile(matlabroot,'toolbox','matlabxl','examples','xlbasic'),'xlbasic')

    At the MATLAB command prompt, navigate to the new xlbasic folder in your work folder.

  2. Examine the MATLAB functions doubleit.m, incrementit.m, and powerit.m.

     function output = doubleit(input)
       output = input * 2;
    function output = incrementit(input1, input2)
       output = input1 + input2;
     function output = powerit(input1, input2)
       output = power(input1, input2);
    
  3. Build the Excel add-in with the Library Compiler app or compiler.build.excelAddIn.

    Use the following information for your project:

    Add-in Namemyexcelfunctions
    Class Namemyexcelfunctionsclass
    Files to Compile

    doubleit.m
    incrementit.m
    powerit.m

    For example, if you are using compiler.build.excelAddIn, type:

    buildResults = compiler.build.excelAddIn(["doubleit.m","incrementit.m","powerit.m"], ...
        'AddInName','myexcelfunctions', ...
        'ClassName','myexcelfunctionsclass', ...
        'GenerateVisualBasicFile','on', ...
        'Verbose','on');

    For more details, see the instructions in Create Excel Add-In from MATLAB.

Deploy Add-In to Microsoft Excel

To deploy your add-in, perform the following steps on the target machine.

  1. Copy the following files to the deployment machine:

    • myexcelfunctions_1_0.dll

    • myexcelfunctions.xla or myexcelfunctions.bas

  2. Install and Configure MATLAB Runtime.

  3. At the system command prompt, register myexcelfunctions_1_0.dll from the folder containing your project DLL:

    mwregsvr myexcelfunctions_1_0.dll

    Note

    You need to re-register your DLL file if you move it following its creation.

    Unlike DLL files, Excel files can be moved anywhere at any time.

  4. Open Microsoft Excel with a blank workbook.

  5. From the Developer tab, select Excel Add-Ins.

  6. In the Add-Ins dialog box, click Browse.

  7. Browse to myexcelfunctions.xla and click OK.

  8. Click OK to load the add-in.

    You can now use the doubleit, incrementit, and powerit functions in your spreadsheet.

  9. 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 the values 5, 28, and 49 in cells A1, A2, and A3, respectively.

  10. (Optional) Use the doubleit, powerit, and incrementit functions in your new Microsoft Excel spreadsheets by saving it as a template.

    1. Select File > Save As and select the file type .xlt (Template).

    2. Browse to the Office_Installation_folder\XLSTART folder.

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