create excel add-in from matlab
supported platform: microsoft® windows® only.
this example shows how to use matlab®
compiler™ to generate a microsoft
excel® add-in containing a custom function for use within excel. the function mymagic
returns 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 for matlab compiler. for details, see .
note
to generate the visual basic® files, enable trust access to the vba project object
model in excel. if you do not do this, you can manually create the add-in by importing
the .bas
file into excel.
create function in matlab
in matlab, locate the matlab code that you want to deploy as an excel add-in.
for this example, compile the function mymagic.m
located in
.matlabroot
\toolbox\matlabxl\examples\xlmagic
function y = mymagic(x)
y = magic(x)
at the matlab command prompt, enter mymagic(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
create excel add-in using library compiler app
package the function into an excel add-in using the library compiler app. alternatively, if you want to create an excel add-in from the matlab command window using a programmatic approach, see create excel add-in using compiler.build.exceladdin.
to open the library compiler app, type
librarycompiler
at the matlab prompt.alternatively, on the matlab apps tab, on the far right of the apps section, click the arrow. in application deployment, click library compiler.
in the matlab compiler project window, select excel add-in in the type section.
in the library compiler app project window, specify the files of the matlab application that you want to deploy.
in the exported functions section of the toolstrip, click .
in the add files window, browse to the example folder, and select the function you want to package. click open.
the function is added to the list of exported function files. repeat this step to package multiple files in the same application.
in the packaging options section of the toolstrip, decide whether to include the matlab runtime installer in the generated application by selecting one of the options:
runtime downloaded from web — generate an installer that downloads the matlab runtime and 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 the matlab runtime installer. you can specify the file name of the installer.
note
the first time you select this option, you are prompted to download the matlab runtime installer.
next, define the name of your add-in and verify the class mapping for the
.m
file that you are building into your application.the library name field is automatically populated with
mymagic
as the name of the assembly. rename it asxlmagic
. the same name is followed through in the implementation of the add-in.verify that the function defined in
mymagic.m
is mapped intoxlmagicclass
. double-click on the class to change the class name.
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. see customize the installer.
additional installer options — edit the default installation path for the generated installer and selecting custom logo. see change the 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. see manage required files in compiler project.
files installed for your end user — files that are installed with your application.
additional runtime settings — platform-specific options for controlling the generated executable. see additional runtime settings.
package the application
when you are finished selecting your packaging options, save your library compiler project and generate the packaged application.
click package.
in the save project dialog box, specify the location to save the project.
in the package dialog box, verify that open output folder when process completes is 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
.for more information about the files generated in these folders, see .
the log file
packaginglog.html
contains packaging results.
create excel add-in using compiler.build.exceladdin
as an alternative to the library compiler app, you can create an excel add-in using a programmatic approach. if you have already created an add-in using the library compiler, see install add-in in excel.
in matlab, locate the matlab code that you want to deploy as a standalone application. for this example, compile using the file
mymagic.m
located in
.matlabroot
\toolbox\matlabxl\examples\xlmagicappfile = fullfile(matlabroot,'toolbox','matlabxl','examples','xlmagic','mymagic.m');
build the excel add-in using the
compiler.build.exceladdin
function. 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 the
compiler.build
command by using name-value arguments. for details, see .the object
buildresults
contains information on the build type, generated files, included support packages, and build options.the function generates the following files within a folder named
mymagicexceladdin
in your 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 include matlab runtime or an installer. to create an installer using the
buildresults
object, see .
install add-in in excel
open microsoft excel.
click the file tab, click options, and then click the add-ins category.
in the manage box, click excel add-ins, and then click go. the add-ins dialog box appears.
click browse and locate the add-in
xlmagic.xla
.you are prompted to copy
xlmagic.xla
to theaddins
folder 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.click ok to close the add-ins dialog box
test add-in
select a grid of 3-by-3 cells in the excel workbook.
enter the following custom function in the formula bar:
as you type=mymagic(3)
my
in the formula bar,mymagic
appears as a custom function in excel.press ctrl shift enter on the keyboard.
the selected cells display the following output:
8 1 6 3 5 7 4 9 2
for additional examples, open the file
xlmagic.xls
located in
. this spreadsheet contains three custom vba macros that demonstrate various ways of using the compiled matlab function.matlabroot
\toolbox\matlabxl\examples\xlmagic
distribute add-in
to distribute your add-in to end users, see .
see also
|