Skip to Main Content Skip to Search
Product Documentation

End-to-End Deployment of a MATLAB Function Using the Function Wizard

MATLAB Programmer

RoleKnowledge BaseResponsibilities

MATLAB programmer
  • MATLAB expert

  • No IT experience

  • No access to IT systems

  • Develops models; implements in MATLAB

  • Uses tools to create a component that is used by the COM developer

If you are still in the process of developing a MATLAB function that is not yet ready to be deployed, you may find this example to be an appropriate introduction to using MATLAB Builder EX.

The Function Wizard allows you to iteratively test, develop, and debug your MATLAB function. It does this by invoking MATLAB from the Function Wizard Control Panel.

Developing your function in an interactive environment ensures that it works in an expected manner, prior to deployment to larger-scale applications. Usually, these applications are programmed by the Excel Developer using an enterprise language like Microsoft Visual Basic.

Similar to the Magic Square example, the Prototyping and Debugging example develops a function named mymagic, which wraps a MATLAB function, magic, which computes a magic square, a function with a single multidimensional matrix output.

If your MATLAB function is ready to be deployed and you have already built your add-in and COM component with the Deployment Tool, see Executing Functions and Creating Macros Using the Function Wizard.

Key Tasks for the MATLAB Programmer

TaskReference
1. Review MATLAB Builder EX prerequisites, if you have not already done so.MATLAB Builder EX Prerequisites
2. Prepare to run the example by copying the example files.Example File Copying
3. Test the MATLAB function you want to deploy as an add-in or COM component.mymagic Testing
4. Install the Function Wizard.Installation of the Function Wizard
5. Start the Function Wizard.Function Wizard Start-Up
6. Select the prototyping and debugging workflow.Workflow Selection for Prototyping and Debugging MATLAB Functions
7. Define the new MATLAB function you want to prototype by adding it to the Function Wizard and establishing input and output ranges.New MATLAB Function Definition
8. Test your MATLAB function by executing it with the Function Wizard.Function Execution from MATLAB
9. Prototype and Debug the MATLAB function if needed, using MATLAB and the Function Wizard.MATLAB Function Prototyping and Debugging
10. Create the add-in and COM component, as well as the macro, using the Function Wizard to invoke MATLAB and the Deployment Tool.Microsoft Excel Add-in and Macro Creation Using the Function Wizard
11. Execute the your function from the newly created component, to ensure the function's behavior is identical to when it was tested.Function Execution from the Deployed Component
12. Execute the macro you created using the Function Wizard.Macro Execution
13. Package your deployable add-in and macro using the Function Wizard to invoke MATLAB and the Deployment Tool.Microsoft Excel Add-in and Macro Packaging using the Function Wizard
14. Optionally inspect or modify the Microsoft Visual Basic code you generated with the COM component. Optionally, attach the macro you created to a GUI button.Microsoft Visual Basic Code Access (Optional Advanced Task)

What Can the Function Wizard Do for Me?

The Function Wizard enables you to pass Microsoft Excel (Excel 2000 or later) worksheet values to a compiled MATLAB model and then return model output to a cell or range of cells in the worksheet.

The Function Wizard provides an intuitive interface to Excel worksheets. You do not need previous knowledge of Microsoft Visual Basic for Applications (VBA) programming.

The Function Wizard reflects any changes that you make in the worksheets, such as range selections. You also use the Function Wizard to control the placement and output of data from MATLAB functions to the worksheets.

Example File Copying

All MATLAB Builder EX examples reside in matlabroot\toolbox\matlabxl\examples\. The following table identifies examples by folder:

For Example Files Relating To...Find Example Code in Folder...For Example Documentation See...
Magic Square Example xlmagicCreating a Microsoft Excel Add-In and COM Component
Integrating Your Add-In and COM Component with Microsoft Excel
Variable-Length Argument ExamplexlmultiWorking with Variable-Length Inputs and Outputs
Calling Compiled MATLAB Functions from Microsoft ExcelxlbasicCalling Compiled MATLAB Functions from Microsoft Excel
Spectral Analysis ExamplexlspectralBuilding and Integrating a COM Component Using Microsoft Visual Basic: the Spectral Analysis Example

mymagic Testing

In this example, you test a MATLAB file (mymagic.m) containing the predefined MATLAB function magic. You test to have a baseline to compare to the results of the function when it is ready to deploy.

  1. In MATLAB, locate mymagic.m. See Example File Copying for locations of examples. The contents of the file are as follows:

    function y = mymagic(x)
    %MYMAGIC Magic square of size x.
    %   Y = MYMAGIC(X) returns a magic square of size x.
    %   This file is used as an example for the MATLAB 
    %   Builder EX product.
    
    %   Copyright 2001-2012 The MathWorks, Inc.
    %   $Revision: 1.1.8.3 $     $Date: 2011/09/03 19:15:45 $
    
    y = magic(x)
    
  2. At the MATLAB command prompt, enter magic(5). View the resulting output, which appears as follows:

    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

Installation of the Function Wizard

Before you can use the Function Wizard, you must first install it as an add-in that is accessible from Microsoft Excel.

After you install the Function Wizard, the entry MATLAB Functions appears as an available Microsoft Excel add-in button.

Using Office 2007

  1. Start Microsoft Excel if it is not already running.

  2. Click the Office Button ( ) and select Excel Options.

  3. In the left pane of the Excel Options dialog box, click Add-Ins.

  4. In the right pane of the Excel Options dialog box, select Excel Add-ins from the Manage drop-down box.

  5. Click Go.

  6. Click Browse. Navigate to install_root\toolbox\matlabxl\matlabxl\arch and select FunctionWizard2007.xlam. Click OK.

  7. In the Excel Add-Ins dialog box, verify that the entry MATLAB® Builder™ EX Function Wizard is selected. Click OK.

Using Office 2010

  1. Click the File tab.

  2. On the left navigation pane, select Options.

  3. In the Excel Options dialog box, on the left navigation pane, select Add-Ins.

  4. In the Manage drop-down, select Excel Add-Ins, and click Go.

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

  6. Browse to install_root/toolbox/matlabxl/matlabxl/arch, and select FunctionWizard2007.xlam. Click OK.

  7. In the Excel Add-Ins dialog, verify that the entry MATLAB® Builder™ EX Function Wizard is selected. Click OK.

The Home tab of the Microsoft Office Ribbon should now contain the Function Wizard tile. See The Home Tab of the Microsoft Office Ribbon with Function Wizard Installed.

Using Office 2003

  1. Select Tools > Add-Ins from the Excel main menu.

  2. If the Function Wizard was previously installed, MATLAB® Builder™ EX Function Wizard appears in the list. Select the item, and click OK.

    If the Function Wizard was not previously installed, click Browse and navigate to install_root\toolbox\matlabxl\matlabxl folder. Select FunctionWizard.xla. Click OK to proceed.

Function Wizard Start-Up

Start the Function Wizard in one of the following ways. When the wizard has initialized, the Function Wizard Start Page dialog box displays.

Using Office 2007 or Office 2010

In Microsoft Excel, on the Microsoft Office ribbon, on the Home tab, select MATLAB Functions.

The Home Tab of the Microsoft Office Ribbon with Function Wizard Installed

Using Office 2003

  1. Select Tools > Add-Ins from the Excel main menu.

  2. Select Function Wizard.

The Function Wizard Start Page Dialog Box

Workflow Selection for Prototyping and Debugging MATLAB Functions

After you have installed and started the Function Wizard, do the following.

  1. From the Function Wizard Start Page dialog box, select I want to create, debug, build and package functions using MATLAB Builder EX (MATLAB installation required). The New Project option is selected by default. Enter a meaningful project name in the Project Name field, like testmymagic, for example.

      Tip   Some customers find it helpful to assign a unique name as the Class Name (default is Class1) and to assign a Version number for source control purposes.

  2. Click OK. The Function Wizard Control Panel displays.

About Project Files

Keep in mind the following information about project files when working with the Function Wizard:

Quitting the MATLAB Session Invoked by the Function Wizard

Avoid manually terminating the MATLAB session invoked by the Function Wizard. Doing so can prevent you from using the Wizard's MATLAB-related features from your Excel session. If you want to quit the remotely invoked MATLAB session, restart Excel.

New MATLAB Function Definition

  1. Add the function you want to deploy to the Function Wizard. Click Add in the Set Up Functions area of the Function Wizard Control Panel. The New MATLAB Function dialog box appears.

  2. Browse to locate your MATLAB function. Select the function and click Open.

  3. In the New MATLAB Function dialog box, click Add. The Function Properties dialog box appears.

  4. Define input argument properties as follows.

    1. On the Input tab, click Set Input Data. The Input Data for n dialog box appears.

    2. Specify a Range or Value by selecting the appropriate option and entering the value.

    3. Click Done.

      Tip   To specify how MATLAB Builder EX handles blank cells (or cells containing no data), see Empty Cell Value Control.

  5. Define output argument properties as follows.

    1. On the Output tab, click Set Output Data. The Output Data for y dialog box appears, where x is the name of the output variable you are defining properties of.

        Tip   You can also specify MATLAB Builder EX to Auto Resize, Transpose or output your data in date format (Output as date). To do so, select the appropriate option in the Argument Properties For y dialog box.

    2. Specify a Range. Alternately, select a range of cells on your Excel sheet; the range will be entered for you in the Range field.

    3. Select Auto Resize if it is not already selected.

    4. Click Done in the Argument Properties For y dialog box.

    5. Click Done in the Function Properties dialog box. mymagic now appears in the Active Functions list of the Function Wizard Control Panel.

Empty Cell Value Control

You can specify how MATLAB Builder EX processes empty cells, allowing you to assign undefined or unrepresented (NaN, for example) data values to them.

To specify how to handle empty cells, do the following.

  1. Click Options in the Input Data for N dialog box.

  2. Click the Treat Missing Data As drop-down box.

  3. Specify either Zero or NaN (Not a Number), depending on how you want to handle empty cells.

Working with Struct Arrays

To assign ranges to fields in a struct array, do the following:

  1. If you have not already done so, select Argument is a MATLAB Structure Array in the Input Data for n dialog box and click Done.

    The Input Data for Structure Array Argument n dialog box opens.

  2. The Function Wizard supports struct arrays organized in the following manner:

    • Vector struct arrays in element-by-element organization (input and output)

    • Two-dimensional struct arrays in element-by-element organization (input and output)

    In the Input Data for Structure Array Argument n dialog box, click Add to add fields for each of your struct array arguments. The Field for Structure Array Argument dialog box opens.

  3. In the Field for Argument dialog box, do the following:

    1. In the Name field, define the field name. The Name you specify must match the name of your MATLAB function.

    2. Specify the Range for the field.

    3. Click Done.

How Structure Arrays are Supported.  MATLAB Builder EX supports one and two-dimensional MATLAB structure arrays.

The product converts data passed into structure arrays in element-by-element organization. If you need to work with structure arrays in plane organization, first define the function to use structure arrays in element-by-element organization as the function's inputs and outputs. Then, convert structure arrays in MATLAB code from element-by-element to plane organization. See MATLAB Programming Fundamentals for more information about all MATLAB data types, including structures.

Deploying Structure Arrays as Inputs and Outputs.  If you are a MATLAB programmer and want to deploy a MATLAB function with structure arrays as input or output arguments, build Microsoft Excel macros using the Function Wizard and pass them (with the Excel Add-In and COM component) to the end users. If you can't do this, let your end users know:

Using Macros with Struct Arrays.  The macro generation feature of MATLAB Builder EX works with struct arrays as input or output arguments. See Macro Creation if you have a MATLAB function you are ready to deploy. See Microsoft Excel Add-in and Macro Creation Using the Function Wizard if you are using the Function Wizard to create your MATLAB function from scratch. See Choosing the Appropriate Workflow for more information on both workflows.

MATLAB Function Prototyping and Debugging

Use the Function Wizard to interactively prototype and debug a MATLAB function.

Since mymagic calls the prewritten MATLAB magic function directly, it does not provide an illustrative example of how to use the prototyping and debugging feature of MATLAB Builder EX.

Following is an example of how you might use this feature with myprimes, a function containing multiple lines of code.

Prototyping and Debugging with myprimes

For example, say you are in the process of prototyping code that uses the equation P = myprimes(n). This equation returns a row vector of prime numbers less than or equal to n (a prime number has no factors other than 1 and the number itself).

Your code uses P = myprimes(n) as follows:

function [p] = myprimes(n)

if length(n)~=1, error('N must be a scalar'); end
if n < 2, p = zeros(1,0); return, end
p = 1:2:n;
q = length(p);
p(1) = 2;
for k = 3:2:sqrt(n)
    
  if p((k+1)/2)
     p(((k*k+1)/2):k:q) = 0;
  end
  
end

p = (p(p>0));

In designing your code, you want to handle various use cases. For example, you want to experiment with scenarios that may assign a column vector value to the output variable p ( (myprimes only returns a row vector, as stated previously). You follow this general workflow:

  1. Set a breakpoint in myprimes at the first if statement, using the GUI or dbstop, for instance.

  2. On the Function Wizard Control Panel, in the Execute Functions area, click Execute. Execution will stop at the previously set breakpoint. Note the value of p in the yellow box in the following figure. Step-through and debug your code as you normally would using the MATLAB Editor.

    Debugging myprimes Using the Function Wizard

For more information about debugging MATLAB code, see Editing and Debugging MATLAB Code in the MATLAB Desktop Tools and Development Environment User's Guide.

Function Execution from MATLAB

Test your deployable MATLAB function by executing it in MATLAB:

  1. From the Function Wizard Control Panel, in the Execute Functions area, select Execute MATLAB Functions in MATLAB.

  2. Click Execute. In Excel, the Magic Square function executes, producing results similar to the following.

Microsoft Excel Add-in and Macro Creation Using the Function Wizard

The Function Wizard can automatically create a deployable Microsoft Excel add-in and macro. To create your add-in in this manner, use one of the following procedures.

Creating an Add-in and Associated Excel Macro

To create both a deployable add-in and an associated Excel macro:

  1. In the Function Wizard Control Panel dialog box, in the Create Component area, select Create Both Builder EX Component and Excel Macro.

  2. Enter mymagic in the Macro Name field.

  3. Select the location of where to store the macro, using the Store Macro In drop-down box.

  4. Enter a brief description of the macro's functionality in the Description field.

  5. Click Create to build both the add-in (as well as the underlying COM component) and the associated macro. The Deployment Tool Build dialog box appears, indicating the status of the add-in and COM component compilation (build).

    The Build Dialog

For More Information About:See:
Troubleshooting compilation (build) problemsCompile-Time Errors in the MATLAB Compiler User's Guide
The build processDeployable Component Creation

Creating a COM Component or a Macro Only Without Creating an Add-in

To create either a COM component or a macro without also creating the Excel add-in, do the following

  1. In the Function Wizard Control Panel dialog box, in the Create Component area, select either MATLAB Builder EX Component Only or Create Excel Macro Only.

  2. Enter mymagic in the Macro Name field.

  3. Select the location of where to store the macro, using the Store Macro In drop-down box.

  4. Enter a brief description of the macro's functionality in the Description field.

  5. Click Create.

Function Execution from the Deployed Component

Execute your function as you did similarly in Function Execution from MATLAB, but this time execute it from the deployed component to ensure it matches your previous output.

  1. From the Function Wizard Control Panel, in the Execute Functions area, select Execute MATLAB Functions from Deployed Component.

  2. Click Execute. In Excel, the Magic Square function executes, producing results similar to the following.

Macro Execution

Run the macro you created in Macro Creation by doing one of the following, after first clearing cells A1:E5 (which contain the output of the Magic Square function you ran in Function Execution).

Using Office 2007 or Office 2010

  1. In Microsoft Excel, click View > Macros > View Macros.

  2. Select mymagic from the Macro name drop-down box.

  3. Click Run. Cells A1:E5 on the Excel sheet are automatically populated with the output of mymagic (a Magic Square of dimension 5).

Using Office 2003

  1. In Microsoft Excel, click Tools > Macro > Macros.

  2. Select mymagic from the Macro name drop-down box.

  3. Click Run. Cells A1:E5 on the Excel sheet are automatically populated with the output of mymagic (a Magic Square of dimension 5).

 Watch a Video Demo

Microsoft Excel Add-in and Macro Packaging using the Function Wizard

The Function Wizard can automatically package a deployable Microsoft Excel add-in and macro for sharing. To package your add-in in this manner, use one of the following procedures.

  1. After successfully building your component and add-in, in the Share Component area of the Function Wizard Control Panel dialog box, review the files listed in the Files to include in packaging field. Add Files or Remove Files to and from the package by clicking the appropriate buttons.

  2. To add access to the MCR Installer to your package, select one of the options in the MCR area. These options are described in Packaging Wizard. For information about the MCR and the MCR Installer, see MATLAB Compiler Runtime (MCR) and the MCR Installer.

  3. When you are ready to create your package, click Create Package.

For More Information About:See:
The packaging processAdd-in Packaging (Recommended) and Package Copying

Microsoft Visual Basic Code Access (Optional Advanced Task)

Optionally, you may want to access the Visual Basic code or modify it, depending on your programming expertise or the availability of an Excel developer. If so, follow these steps.

From the Excel main window, open the Microsoft Visual Basic editor by doing one of the following. select Tools > Macro > Visual Basic Editor.

Using Office 2007 or Office 2010

  1. Click Developer > Visual Basic.

  2. When the Visual Basic Editor opens, in the Project - VBAProject window, double-click to expand VBAProject (mymagic.xls).

  3. Expand the Modules folder and double-click the Matlab Macros module.

    This opens the VB Code window with the code for this project.

Using Office 2003

  1. Click Tools > Macro > Visual Basic Editor.

  2. When the Visual Basic Editor opens, in the Project - VBAProject window, double-click to expand VBAProject (mymagic.xls).

  3. Expand the Modules folder and double-click the Matlab Macros module.

    This opens the VB Code window with the code for this project.

Mapping a Macro to a GUI Button or Control (Optional)

To attach the macro to a GUI button, do the following:

  1. Click Developer > Insert.

  2. From the Form Controls menu, select the Button (Form Control) icon.

      Tip   Hover your mouse over the Form Controls menu to see the various control labels.

  3. In the Assign Macros dialog box, select the macro you want to assign the GUI button to, and click OK.

Attaching a Macro to a Button

For More Information

If you want to...See...
  • Perform basic MATLAB Programmer tasks

  • Understand how the deployment products process your MATLAB functions

  • Understand how the deployment products work together

  • Explore guidelines about writing deployable MATLAB code

MATLAB Code Deployment
See more examples about building add-ins and COM componentsMicrosoft Excel Add-in Creation, Function Execution, and Deployment
Learn more about the MATLAB Compiler Runtime (MCR)Working with the MCR in the MATLAB Compiler User's Guide
Learn how to customize and integrate the COM component you built by modifying the Microsoft Visual Basic codeMicrosoft Excel Add-in Integration

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS