Assign Multiple MATLAB Functions to Excel Class
Create Add-In In MATLAB
Note
In order to call a MATLAB® function using the Microsoft®
Excel® function syntax (=myfunction(input)
), the MATLAB function must return a single scalar output argument. To specify multiple
outputs, create a macro using the Function Wizard or custom VBA code.
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.Examine the MATLAB functions
doubleit.m
,incrementit.m
, andpowerit.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);
Build the Excel add-in with the Library Compiler app or
compiler.build.excelAddIn
.Use the following information for your project:
Add-in Name myexcelfunctions
Class Name myexcelfunctionsclass
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 to end users, see Distribute Add-Ins and Integrate into Microsoft Excel.
After you register the component and install the add-in, you can use the
doubleit
,incrementit
, andpowerit
functions in your spreadsheet.Open Microsoft Excel with a blank workbook.
Test the functions by doing the following:
Enter
=doubleit(2.5)
in cell A1.Enter
=incrementit(11,17)
in cell A2.Enter
=powerit(7,2)
in cell A3.
You should see the values 5, 28, and 49 in cells A1, A2, and A3, respectively.
(Optional) Use the
doubleit
,powerit
, andincrementit
functions in new Microsoft Excel spreadsheets by saving it as a template.Select File > Save As and select the file type .xlt (Template).
Browse to the
folder.Office_Installation_folder
\XLSTARTSave the file as
.Office_Installation_folder
\XLSTART\Book.xltNote
Your Microsoft Excel Macro Security level must be set at Medium or Low to save this template.