Building a Deployable Application

www.kxcad.net Home > CAE Software Index > MATLAB Index >


Your Ad Here

Creating and Building a Component

To use MATLAB Builder for Excel to build a component, follow this procedure:

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

  2. Enter the following MATLAB command at the command line:

    deploytool

    MATLAB opens the Deployment Tool dialog box, shown in the following figure.

  3. Create a new project by clicking the New Project button in the toolbar.

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

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

  6. Add classes (optional).

  7. Save the project by clicking the Save button in the toolbar.

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

  9. Test, edit, and rebuild as necessary.

Overview of MATLAB Compiler

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:

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

What Is the Deployment Tool?

The Deployment Tool is the GUI to MATLAB Compiler. Use the Deployment Tool to perform the tasks in the following illustration.

Using the mcc Command to Build a Component

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.

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

Testing the Component

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:

  1. Open Excel and select Tools > Macros > VisualBasic Editor.

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

Deploying the Component

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.

  1. Start Excel.

  2. Select Tools > Macros > Visual Basic Editor.

  3. In the Microsoft Visual Basic window, select File > Import.

  4. Select VBA file (.bas) from the <projectdir>distrib directory.

  5. Close the Visual Basic Editor.

  6. From the Excel worksheet window, select File > Save As.

  7. Set Save as to Microsoft Excel add-in (*.xla).

  8. Save the .xla file to <projectdir>\distrib.

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.

Packaging and Distributing the Component

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.

FileDescription
<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:

  1. Start Excel.

  2. Click Tools > Add-Ins.

  3. Select the desired .xla file.

  


© 1984-2007 The MathWorks, Inc. Terms of Use Patents Trademarks Acknowledgments

Your Ad Here