Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Builder EX   

Multiple Files and Variable Arguments Example

Overview

The M-file, myplot, takes a single integer input and plots a line from 1 to that number.

The M-file, mysum, takes an input of varargin of type integer, adds all the numbers, and returns the result.

The M-file, myprimes, takes a single integer input n and returns all the prime numbers less than or equal to n.

The Microsoft Excel file, xlmulti.xls, demonstrates these functions in several ways.

Creating the Project

  1. From the MATLAB command prompt, change folders to myfiles\work.

  2. If you have not already done so, execute the following command at the MATLAB prompt:

    mbuild -setup
    

    Be sure to choose a supported compiler. See Supported Compilers.

  3. While in MATLAB, issue the following command to open Deployment Tool:

    deploytool
  4. Use the following information as you work through this example using the instructions in Building Your Component:

    Project Namexlmulti
    Class Namexlmulticlass
    File to compile (in the xlmulti folder ofmyfiles\work)myplot.mmyprimes.mmysum.m

Adding the MATLAB Builder EX COM Function to Microsoft Excel

  1. Start Microsoft Excel on your system.

  2. Open the file myfiles\work\xlmulti\xlmulti.xls.

      Note   If an Excel prompt says that this file contains macros, click Enable Macros to run this example.

    The example appears as shown:

Calling myplot

This illustration calls the function myplot with a value of 4. To execute the function, make A7 (=myplot(4)) the active cell. Press F2 and then Enter.

This procedure plots a line from 1 through 4 in a MATLAB Figure window. This graphic can be manipulated similarly to the way one would manipulate a figure in MATLAB. Some functionality, such as the ability to change line style or color, is not available.

The calling cell contains 0 because the function does not return a value.

Calling mysum Four Different Ways

This illustration calls the function mysum in four different ways:

This illustration runs when the Excel file is opened. To reactivate the illustration, activate the appropriate cell. Then press F2 followed by Enter.

myprimes Macro

In this illustration, the macro myprimes calls the function myprimes.m with an initial value of 10 in cell A42. The function returns all the prime numbers less than 10 to cells B42 through E42.

To execute the macro, from the main Excel window (not the Visual Basic Editor), open the Macro dialog box, by pressing the Alt and F8 keys simultaneously, or by selecting Tools > Macro > Macros.

Select myprimes from the list and click Run.

This function automatically resizes if the returned output is larger than the output range specified. Change the value in cell A42 to a number larger than 10. Then rerun the macro. The output returns all prime numbers less than the number you entered in cell A42.

Inspecting the Microsoft Visual Basic Code

  1. On the Microsoft Excel main window, select Tools > Macro > Visual Basic Editor.

  2. On the Microsoft Visual Basic, in the Project - VBAProject window, double-click to expand VBAProject (xlmulti.xls)

  3. Expand the Modules folder and double-click the Module1 module. This opens the VB Code window with the code for this project.

  


Recommended Products

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

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