| MATLAB Builder for Excel | ![]() |
www.kxcad.net Home > CAE Software Index > MATLAB Index >
| On this page… |
|---|
Creating and Building a Component |
To use MATLAB Builder for Excel to build a component, follow this procedure:
If you have not already done so, enter the following MATLAB command at the command line:
mbuild -setup
Be sure to choose a supported compiler. See Supported Compilers at http://www.mathworks.com/support/tech-notes/1600/1601.shtml.
Enter the following MATLAB command at the command line:
deploytool
MATLAB opens the Deployment Tool dialog box, shown in the following figure.

Create a new project by clicking the New Project button
in the toolbar.
Add files that you want to encapsulate by dragging them to the
Deployment Tool, or by selecting them and clicking the Add Files button
in the toolbar.
Set properties for building and packaging.
When you build you can create a debug version of your compiled models and can specify verbose output. The debug option lets you trace back to the point where you can identify if the failure occurred in the initialization of MCR, the function call, or the termination routine.
Click Settings to view and specify these and other settings or click the Settings button in the toolbar.
Add classes (optional).
Save the project by clicking the Save button
in the toolbar.
Build the component.
Click the Build button
in the toolbar to start the build process.
Excel Builder copies intermediate source files to project_directory\src and output files necessary for deployment (a DLL and a VBA file (.bas) to project_directory\distrib. It also creates a component assembly containing the wrapper class and a component .ctf file in the \distrib subdirectory of your project directory.
The .ctf file (component technology file) is required to support components that encapsulate MATLAB functions when running them on a user machine that does not have the MATLAB desktop installed.
The Output pane shows the output of the build process and informs you of any problems encountered. The resulting DLL is automatically registered on your system.
Test, edit, and rebuild as necessary.
Use MATLAB® Compiler to convert MATLAB® programs to applications and libraries that you can distribute to end users who do not have MATLAB installed. You can compile M-files, MEX-files, or other MATLAB code. MATLAB Builder for Excel supports all the features of MATLAB, including objects, private functions, and methods. Using MATLAB Builder for Excel you can generate the following:
Standalone C and C++ applications on UNIX, Windows, and Macintosh platforms
C and C++ shared libraries (dynamically linked libraries, or DLLs, on Microsoft Windows)
Use the mcc command to invoke MATLAB Builder for Excel. Alternatively, you can use the graphical user interface for MATLAB Builder for Excel by issuing the following command at the MATLAB prompt:
deploytool
The Deployment Tool is the GUI to MATLAB Compiler. Use the Deployment Tool to perform the tasks in the following illustration.

Instead using the Deployment Tool, you can use the mcc command on the MATLAB command line to build Excel Builder components. The following sections provide some examples of using the mcc command. See the MATLAB Compiler documentation for a complete description of the mcc command and its options.
Note If you use mcc, the project_directory\src and project_directory\distrib directories are not automatically created. To create these directories and copy associated files to them, use the mcc command's -d option. |
The following is the general syntax to create Excel Builder components with mcc:
mcc -W 'excel:<component_name>[,<class_name>[,<major>.<minor>]]'
The syntax uses the -W option, specifying an excel wrapper. You must specify the name you want to assign the component (<component_name>). If you do not specify the class name (<class_name>), mcc uses the component name as the default. If you do not specify a version number, mcc uses the latest version built or 1.0, if there is no previous version.
The following example shows the mcc command used to create a COM component called mycomponent containing single COM class named myclass with methods foo and bar, and a version of 1.0. The -T option tells mcc to create a DLL.
mcc -W 'excel:mycomponent,myclass,1.0' -T link:lib foo.m bar.m
To generate an Excel-compatible formula function for each M-file, specify the -b option on the command line, as follows:
mcc -W 'excel:mycomponent,myclass,1.0' -b -T link:lib foo.m bar.m
As an alternative, you can also use the cexcel bundle file to simplify the command line. In the example, note how you do not need to specify the -T or the -b options.
mcc -B 'cexcel:mycomponent,myclass,1.0' foo.m bar.m
After you build a component, you can test your software by importing the VBA file (.bas) into the Excel Visual Basic Editor and invoking one of the functions from the Excel worksheet. To import the VBA code into Excel's Visual Basic Editor:
Open Excel and select Tools > Macros > VisualBasic Editor.
From the Visual Basic Editor, select File > Import and select the created VBA file from the <project_dir>\distrib directory.
The Visual Basic module created when you build the project contains the necessary initialization code and a VBA formula function for each MATLAB function processed. Each supplied formula function wraps a call to the respective compiled function in a format that can be accessed from a cell in an Excel worksheet. The function takes a list of inputs corresponding to the inputs of the original MATLAB function and returns a single output corresponding to the first output argument.
Formula functions of this type are most useful to access a function of one or more inputs that returns a single scalar value. When you require multiple outputs or outputs representing ranges of data, you need a more general Visual Basic subroutine. For details about integrating Excel Builder components into Microsoft Excel via Visual Basic for Applications, see Programming with MATLAB Builder for Excel.
After you create and test your component, you then create an Excel add-in (.xla) from the VBA code generated by Excel Builder by saving the worksheet file as an .xla file to the <project_dir>\distrib directory.
For more information about creating an Excel Add-in, refer to the Excel documentation on creating a .xla file.
You can also deploy files in default Excel file format and *.bas formats. To deploy in default Excel file format, follow the previous steps but change the Save as type in step 7 to the default Excel file format. To deploy as VBA code, follow steps 1 to 4 only.
After you have successfully compiled your models and created the Excel
add-in, you can package the component for distribution to your end users by
reopening the project in Deployment Tool and clicking the Package button
in the toolbar. Run the generated
self-extracting executable on the target machine and repeat for each additional
target machine.
MATLAB Builder for Excel creates a self-extracting executable containing the following files.
| File | Description |
|---|---|
| <componentname>.ctf | Component Technology File archive; platform-dependent file that must correspond to the end user's platform |
| <componentname_projectversion>.dll | Compiled component |
| _install.bat | Script run by the self-extracting executable |
| MCRInstaller.exe | Self-extracting MATLAB Component Runtime library utility that installs the MCR; platform-dependent file that must correspond to the end user's platform; you must install on the target machine once per release. To include the MCR, ensure you select the include MCR option in the project settings. See Working with the MCRfor more information. |
| *.xla | Any Excel add-in files found in the <projectdir>\distrib directory |
To use the Excel add-ins:
Start Excel.
Click Tools > Add-Ins.
Select the desired .xla file.
| What Is MATLAB Builder for Excel? | Examples | ![]() |
© 1984-2007 The MathWorks, Inc. Terms of Use Patents Trademarks Acknowledgments