Skip to Main Content Skip to Search
Product Documentation

Using MATLAB Functions in Microsoft Excel

How Spreadsheet Link EX Functions Differ from Microsoft Excel Functions

Types of Spreadsheet Link EX Functions

Spreadsheet Link EX functions manage the connection and data exchange between the Excel software and the MATLAB workspace, without your ever needing to leave the Excel environment. You can run functions as worksheet cell formulas or in macros. The Spreadsheet Link EX software enables the Excel product to act as an easy-to-use data-storage and application-development front end for the MATLAB software, which is a powerful computational and graphical processor.

There are two types of Spreadsheet Link EX functions: link management functions and data management functions.

Link management functions initialize, start, and stop the Spreadsheet Link EX and MATLAB software. You can run any link management function other than matlabinit as a worksheet cell formula or in macros. You must run the matlabinit function from the Excel Tools > Macro menu, or in macro subroutines.

Data management functions copy data between the Excel software and the MATLAB workspace, and execute MATLAB commands in the Excel interface. You can run any data management function other than MLPutVar and MLGetVar as a worksheet cell formula or in macros. The MLPutVar and MLGetVar functions can run only in macros.

For more information about Spreadsheet Link EX functions, see Function Reference.

Using Worksheets

Entering Functions into Worksheet Cells

Spreadsheet Link EX functions expect A1-style worksheet cell references; that is, columns designated with letters and rows with numbers (the default reference style). If your worksheet shows columns designated with numbers instead of letters:

  1. Click Tools > Options.

  2. Click the General tab.

  3. Under Settings, clear the R1C1 reference style check box.

Enter Spreadsheet Link EX functions directly into worksheet cells as worksheet formulas. Begin worksheet formulas with + or = and enclose function arguments in parentheses. The following example uses MLPutMatrix to put the data in cell C10 into matrix A:

=MLPutMatrix("A", C10)

For more information on specifying arguments in Spreadsheet Link EX functions, see Working with Arguments in Spreadsheet Link EX Functions.

After a Spreadsheet Link EX function successfully executes as a worksheet formula, the cell contains the value 0. While the function executes, the cell might continue to show the formula you entered.

To change the active cell when an operation completes, click Excel Tools Options > Edit > Move Selection after Enter. This action provides a useful confirmation for lengthy operations.

Automatic Calculation Mode Vs. Manual Calculation Mode

Spreadsheet Link EX functions are most effective in automatic calculation mode. To automate the recalculation of a Spreadsheet Link EX function, add to it a cell whose value changes. In the following example, the MLPutMatrix function reexecutes when the value in cell C1 changes:

=MLPutMatrix("bonds", D1:G26) + C1

To use MLGetMatrix in manual calculation mode:

  1. Enter the function into a cell.

  2. Press F2.

  3. Press Enter. The function executes.

Spreadsheet Link EX functions do not automatically adjust cell addresses. If you use explicit cell addresses in a function, you must edit the function arguments to reference a new cell address when you do either of the following:

Working with Arguments in Spreadsheet Link EX Functions

This section describes tips for managing variable-name arguments and data-location arguments in Spreadsheet Link EX functions.

Variable-Name Arguments

Data-Location Arguments

Using the MATLAB Function Wizard for the Spreadsheet Link EX Software

The MATLAB Function Wizard for the Spreadsheet Link EX software allows you to browse MATLAB folders and run functions from within the Excel interface.

You can use this wizard to:

  1. Display a list of all MATLAB working folders and function categories.

    All folders or categories in the current MATLABPATH display in the Select a category field. Click an entry in the list to select it. Each entry in the list displays as a folder path and a description read from the Contents.m file in that folder. If no Contents.m file is found, the folder or category display notifies you as follows:

    finance\finsupport -(No table of contents file)

    To refresh the folder/category list, click the Update button.

  2. Select a particular folder or category, and list functions available for that folder or category.

    After you select a folder or category, the Select a function field displays available functions for that folder or category. Click a function name to select it.

  3. Select a function signature and enter a formula into the current spreadsheet cell.

    After you select a function, the Select a function signature field displays available signatures for that function. Click a function signature to select it.

  4. View help for the selected function.

    The Function Help field displays help for the selected function.

When you click a function signature, the Function Arguments dialog box appears.

This dialog box allows you to specify the cells that contain input arguments and the cells where to display outputs. By default, the output of the selected function appears in the current spreadsheet cell using the Spreadsheet Link EX function matlabfcn. In the following example, the output displays in the current spreadsheet cell and generates a MATLAB figure:

=matlabfcn("plot",Sheet1!$B$2:$D$4)

Specifying a target range of cells using the Optional output cell(s) field in the Function Arguments dialog box causes the selected function to appear in the current spreadsheet cell as an argument of the matlabsub function. In addition, matlabsub includes an argument that indicates where to write the function's output. In the following example, the data from A2 is input to the rand function, whose target cell is B2:

=matlabsub("rand","Sheet1!$B$2",Sheet1!$A$2)

For example, if a function returns two separate elements a and b, and you specify A1:A2 as output cells, the Function Wizard displays element a in cell A1. It discards element b. If an output is a matrix, the Function Wizard displays all elements of that matrix starting in the first output cell.

Using the Function Wizard to Access Custom MATLAB Functions

To access your custom MATLAB functions from the Function Wizard:

  1. In MATLAB, create and save your function. For example, write the function that computes the Fibonacci numbers and save it in the folder Documents\MATLAB:

    function f = fibonacci(n)
    %FIBONACCI(N)  Compute the Nth Fibonacci number.
    % N must be a positive integer.
    if n < 0
        error('Invalid number.')
    elseif n == 0
        f = 0;
    elseif n == 1
        f = 1;
    else
        f = fibonacci(n - 1) + fibonacci(n - 2);
    end;
    end
  2. Add the folder where you saved the function to the MATLAB search path. To add the folder to the search path, use the pathtool function or select File > Set Path in the MATLAB desktop.

  3. In Excel, open the MATLAB Function Wizard and select the folder where you saved your function.

The Function Wizard does not allow you to access MATLAB constructors and methods. To be able to access a method or a constructor from the Function Wizard, write a wrapper function for that method or constructor. For example, to access the timeseries(DATA) constructor from the Function Wizard, write the following wrapper function:

function TS = timeseries_wrapper(DATA)
% timeseries_wrapper(DATA) is a wrapper function
% for TIMESERIES(DATA)
%   TS = TIMESERIES(DATA) creates a time series object TS using
%   data DATA. By default, the time vector ranges from 0 to N-1,
%   where N is the number of samples, and has an interval of 1 
%   second. The default name of the TS object is 'unnamed'.  
T = timeseries(DATA);
TS = T.data;
end

Examples: Using Spreadsheet Link EX Functions in Macros

About the Examples

This section contains examples that show how to manipulate MATLAB data using Spreadsheet Link EX.

Sending MATLAB Data to an Excel Worksheet and Displaying the Results

In this example, you run MATLAB commands using VBA, send MATLAB data to the Excel software, and display the results in an Excel dialog box.

  1. Start an Excel session.

  2. Initialize the MATLAB session by clicking the startmatlab button in the Spreadsheet Link EX toolbar or by running the matlabinit function.

  3. If the Spreadsheet Link EX Add-In is not enabled, enable it.

  4. Enable the Spreadsheet Link EX software as a Reference in the Microsoft Visual Basic Editor. For instructions, see Working with the Microsoft Visual Basic Editor.

  5. In the Visual Basic Editor, create a module.

    1. Right-click the Microsoft Excel Objects folder in the Project — VBAProject browser.

    2. Select Insert > Module.

  6. Enter the following code into the module window:

    Option Base 1
    Sub Method1()
    
        MLShowMatlabErrors "yes"
         
        '''To MATLAB:
        Dim Vone(2, 2) As Double    'Input
        Vone(1, 1) = 1
        Vone(1, 2) = 2
        Vone(2, 1) = 3
        Vone(2, 2) = 4
         
        MLPutMatrix "a", Range("A1:B2")
        MLPutVar "b", Vone
        MLEvalString ("c = a*b")
        MLEvalString ("d = eig(c)")
         
        '''From MATLAB:
        Dim Vtwo As Variant         'Output
        MLGetVar "c", Vtwo
        MsgBox "c is " & Vtwo(1, 1)
         
        MLGetMatrix "b", Range("A7:B8").Address
        MatlabRequest
        MLGetMatrix "c", "Sheet1!A4:B5"
        MatlabRequest
    
        Sheets("Sheet1").Select
        Range("A10").Select
        MLGetMatrix "d", ActiveCell.Address
        MatlabRequest
         
    End Sub
    

      Tip   Copy and paste this code into the Visual Basic Editor from the HTML version of the documentation.

  7. Run the code. Press F5 or click Run > Run Sub/UserForm.

    The following dialog box appears.

  8. Click OK to close the dialog box.

Importing and Exporting Data Between the Microsoft Excel Interface and the MATLAB Workspace

  


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