You are on page 1of 78

Excel Link

For Use with MATLAB

Computation Visualization Programming

Users Guide
Version 2

How to Contact The MathWorks:

Web Newsgroup www.mathworks.com/contact_TS.html Technical support


www.mathworks.com comp.soft-sys.matlab suggest@mathworks.com bugs@mathworks.com doc@mathworks.com service@mathworks.com info@mathworks.com

Product enhancement suggestions Bug reports Documentation error reports Order status, license renewals, passcodes Sales, pricing, and general information

508-647-7000 (Phone) 508-647-7001 (Fax) The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098
For contact information about worldwide offices, see the MathWorks Web site. Excel Link Users Guide COPYRIGHT 1996 - 2006 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the government's needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc.

Trademarks MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, and xPC TargetBox are registered trademarks of The MathWorks, Inc. Other product or brand names are trademarks or registered trademarks of their respective holders. Patents The MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information.

Revision History
May 1996 May 1997 January 1999 September 2000 April 2001 July 2002 September 2003 June 2004 September 2005 March 2006 First printing Second printing Third printing Fourth printing Fifth printing Sixth printing Online only Online only Online only Online only New for Version 1.0 Updated for Version 1.0.3 Updated for Version 1.0.8 (Release 11) Updated for Version 1.1.2 Updated for Version 1.1.3 Updated for Version 2.0 (Release 13) Updated for Version 2.1 (Release 13SP1) Updated for Version 2.2 (Release 14) Updated for Version 2.3 (Release 14SP3) Updated for Version 2.3.1 (Release 2006a)

Contents
Getting Started

1
What Is Excel Link? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Understanding the Environment . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Installing and Operating Excel Link . . . . . . . . . . . . . . . . . . . . System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing Excel Link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Configuring Excel to Work with Excel Link . . . . . . . . . . . . . . . Starting Excel Link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Automatic Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Manual Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Connecting to an Existing MATLAB Session . . . . . . . . . . . . . . Stopping Excel Link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-3 1-3 1-3 1-3 1-5 1-5 1-5 1-5 1-5

What the Functions Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7 Link Management Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7 Data Management Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8 Tips and Reminders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9 Function Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9 Worksheet Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9 Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9 Worksheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11 Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11 Saved Worksheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12 Information for International Users . . . . . . . . . . . . . . . . . . . . 1-13

Using Excel Link

2
Example 1: Regression and Curve Fitting . . . . . . . . . . . . . . . 2-3 Worksheet Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 Macro Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6 Example 2: Interpolating Data . . . . . . . . . . . . . . . . . . . . . . . . . 2-9 Example 3: Pricing a Stock Option with the Binomial Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13 Example 4: Calculating and Plotting the Efficient Frontier of Financial Portfolios . . . . . . . . . . . . . . . 2-16 Example 5: Bond Cash Flow and Time Mapping . . . . . . . . . 2-20

Function Reference

3
Functions - By Category . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 Link Management Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 Data Management Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 Functions Alphabetical List . . . . . . . . . . . . . . . . . . . . . . . . . 3-4

Error Messages and Troubleshooting

A
Excel Cell Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-2

ii

Contents

Excel Error Message Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-5 Audible Error Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-7 Data Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-8

Installed Files

B
Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-2

Index

iii

iv

Contents

1
Getting Started

What Is Excel Link? (p. 1-2) Installing and Operating Excel Link (p. 1-3) What the Functions Do (p. 1-7) Tips and Reminders (p. 1-9)

How Excel Link works with both MATLAB and Excel. How to make Excel Link work with Excel after installation. Describes the two kinds of Excel Link functions -- Link Management and Data management. Miscellaneous details concerning product use.

Getting Started

What Is Excel Link?


Excel Link is a software add-in that integrates Microsoft Excel and MATLAB in a Microsoft Windows-based computing environment. By connecting Excel and MATLAB, you can access the numerical, computational, and graphical power of MATLAB from Excel worksheet and macro programming tools. Excel Link lets you exchange and synchronize data between the two environments.

Understanding the Environment


Excel Link communicates between the Excel workspace and the MATLAB workspace. It positions Excel as a front end to MATLAB. You use Excel Link functions from an Excel worksheet or macro, and you never have to leave the Excel environment. With a small number of functions to manage the link and manipulate data, Excel Link is powerful in its simplicity.

1-2

Installing and Operating Excel Link

Installing and Operating Excel Link


Follow these instructions to install Excel Link and then configure Excel.

System Requirements
Excel Link requires approximately 202 kilobytes of disk space. Operating system requirements are Microsoft Windows XP Microsoft Windows 2000 Excel Link also requires one of the following versions of Excel: Excel 2000 Excel 2002 Excel 2003 and MATLAB for Windows. For best results with MATLAB figures and graphics, set the color palette of your display to a value greater than 256 colors. Click Start, then Settings and Control Panel. Open Display, and on the Settings tab, choose an appropriate entry from the Color Palette menu.

Installing Excel Link


Install Windows and Excel before you install MATLAB and Excel Link. To install Excel Link, follow the instructions in the MATLAB installation documentation. Click in the box for Excel Link when you select MATLAB components to install.

Configuring Excel to Work with Excel Link


Once you have installed Excel Link, you are ready to configure Excel. You need do these steps only once:
1 Start Microsoft Excel. 2 Pull down the Tools menu, select Add-Ins and click Browse.

1-3

Getting Started

3 Find and select the Excel Link add-in excllink.xla under

<matlab>/toolbox/exlink. Click OK.

Note Throughout this document the notation <matlab> represents the MATLAB root directory, the directory where MATLAB is installed on your system.

4 Back in the Add-Ins window, make sure there is a check in the box for Excel

Link for use with MATLAB and click OK. The Excel Link add-in loads now and with each subsequent invocation of Excel.
5 Watch for the appearance of the MATLAB Command Window button on

the Windows taskbar.


6 Watch for the appearance of the Excel Link toolbar on your Excel worksheet.

Start MATLAB and Display Command Window

Send data to MATLAB

Retrieve MATLAB matrix

Execute MATLAB command

Excel Link is now ready for your use.

1-4

Installing and Operating Excel Link

Starting Excel Link


Automatic Start
When installed and configured according to the preceding instructions, Excel Link and MATLAB automatically start when you start Excel. If you do not want Excel Link and MATLAB to start automatically when you start Excel, enter =MLAutoStart("no") in a worksheet cell. This function changes the initialization file so that Excel Link and MATLAB no longer start automatically when you start Excel. See MLAutoStart in Chapter 3, Function Reference.

Manual Start
To start Excel Link and MATLAB manually from Excel, pull down the Tools menu and select Macro. In the Macro Name/Reference box enter matlabinit and click Run. Watch for the MATLAB Command Window button to appear on the taskbar. See matlabinit in Chapter 3, Function Reference.

Connecting to an Existing MATLAB Session


To connect a new Excel session to an existing MATLAB process, you must start MATLAB with the /automation command line option. The /automation option starts MATLAB as an automation server. The Command Window is minimized, and the MATLAB desktop is not running. To add the /automation option to the command line,
1 Right-click on your shortcut to MATLAB. 2 Select Properties. 3 Click on the Shortcut tab. 4 Add the string /automation in the Target field. Remember to leave a space

between matlab.exe and /automation.

Stopping Excel Link


To stop both Excel Link and MATLAB, stop Excel as you normally would. Excel Link and MATLAB both stop when you stop Excel.

1-5

Getting Started

To stop MATLAB and Excel Link and leave Excel running, enter =MLClose() in an Excel worksheet cell. You can restart Excel Link and MATLAB manually with MLOpen or matlabinit. If you stop MATLAB directly in the MATLAB Command Window and leave Excel running, enter =MLClose() in an Excel worksheet cell. (MLClose tells Excel that MATLAB is no longer running.) You can restart Excel Link and MATLAB manually with MLOpen or matlabinit.

1-6

What the Functions Do

What the Functions Do


With Excel Link, Microsoft Excel becomes an easy-to-use data-storage and application-development front end for MATLAB, which is a powerful computational and graphical processor. Excel Link provides functions to manage the link and to manipulate data. You never have to leave the Excel environment. You can invoke functions as worksheet cell formulas or in macros. See Chapter 3, Function Reference for details on each function.

Link Management Functions


Excel Link provides four link management functions to initialize, start, and stop Excel Link and MATLAB.
Function matlabinit MLAutoStart MLClose MLOpen Purpose

Initialize Excel Link and start MATLAB process. Automatically start MATLAB process. Terminate MATLAB process. Start MATLAB process.

You can invoke any link management function except matlabinit as a worksheet cell formula or in a macro. You invoke matlabinit from the Excel Tools Macro menu or in a macro subroutine. Use MLAutoStart to toggle automatic startup. If you install and configure Excel Link according to the default instructions, Excel Link and MATLAB automatically start every time you start Excel. If you choose manual startup, use matlabinit to initialize Excel Link and start MATLAB. Use MLClose to stop MATLAB without stopping Excel, and use MLOpen or matlabinit to restart MATLAB in the same Excel session.

1-7

Getting Started

Data Management Functions


Excel Link provides nine data management functions to copy data between Excel and MATLAB and to execute MATLAB commands from Excel.
Function matlabfcn matlabsub MLAppendMatrix MLDeleteMatrix MLEvalString MLGetMatrix MLGetVar MLPutMatrix MLPutVar Purpose

Evaluate MATLAB command given Excel data. Evaluate MATLAB command given Excel data and designate output location. Create or append MATLAB matrix with data from Excel worksheet. Delete MATLAB matrix. Evaluate command in MATLAB. Write contents of MATLAB matrix in Excel worksheet. Write contents of MATLAB matrix in Excel VBA variable. Create or overwrite MATLAB matrix with data from Excel worksheet. Create or overwrite MATLAB matrix with data from Excel VBA variable.

You can invoke any data management function except MLGetVar and MLPutVar as a worksheet cell formula or in a macro. You can invoke MLGetVar and MLPutVar only in a macro. Use MLAppendMatrix, MLPutMatrix, and MLPutVar to copy data from Excel to MATLAB. Use MLEvalString to execute MATLAB commands from Excel. Use MLDeleteMatrix to delete a MATLAB variable. Use matlabfcn, matlabsub, MLGetMatrix and MLGetVar to copy data from MATLAB to Excel.

1-8

Tips and Reminders

Tips and Reminders


These tips and reminders help you use Excel Link efficiently. Excel Link functions perform an action, while Microsoft Excel functions return a value. Keep this distinction in mind as you use Excel Link. Excel operations and function keys may behave differently with Excel Link functions.

Syntax
Function Names
Excel Link function names are not case sensitive; that is, MLPutMatrix and mlputmatrix are the same. MATLAB function names and variable names are case sensitive; that is, BONDS, Bonds, and bonds are three different MATLAB variables. Standard MATLAB function names are always lower case; for example, plot(f).

Worksheet Formulas
Begin worksheet formulas with + or =. For example,
=mlputmatrix("a", C10)

In worksheet formulas, enclose function arguments in parentheses. In macros, leave a space between the function name and the first argument; do not use parentheses.

Variable Names
You can directly or indirectly specify a variable-name argument in most Excel Link functions. - To specify a variable name directly, enclose it in double quotes; for example, MLDeleteMatrix("Bonds"). - A variable-name argument without quotes is an indirect reference. The function evaluates the contents of the argument to get the variable name. The argument must be a worksheet cell address or range name. A data-location argument must be a worksheet cell address or range name. Do not enclose a data-location argument in quotes (except in MLGetMatrix, which has unique argument conventions).

1-9

Getting Started

A data-location argument can include a worksheet number; for example, Sheet3!B1:C7 or Sheet2!OUTPUT.

Note You can use virtually any special character as part of a worksheet name if you embed the sheet name within single quotes ('') when referencing it in MLGetMatrix or MLPutMatrix.

Worksheets
After an Excel Link function successfully executes as a worksheet formula, the cell contains the value 0. While a function is executing, the cell may continue to show the entered formula. We suggest selecting Move Selection after Enter on the Excel Tools Options -> Edit tab. The active cell changes when an operation is complete, providing a useful confirmation for lengthy operations. We recommend using Excel Link functions in automatic calculation mode. If you use MLGetMatrix in manual calculation mode, enter the function in a cell, then press F9 to execute it. However, pressing F9 in this situation may also reexecute other worksheet functions and generate unpredictable results. To recalculate Excel Link functions in a worksheet, reexecute each function by pressing F2, then Enter. Pressing F9 to recalculate a worksheet affects only Excel functions (which return a value). F9 does not operate on Excel Link functions, which perform an action. To automate the recalculation of an Excel Link function, add to it some cell whose value changes. For example:
=MLPutMatrix("bonds", D1:G26) + C1

When the value in cell C1 changes, Excel re-executes the MLPutMatrix function. Be careful, however, not to create endless recalculation loops. Excel Link functions expect A1-style worksheet cell references (columns designated with letters and rows with numbers). This is the default reference style. If your worksheet shows columns designated with numbers

1-10

Tips and Reminders

instead of letters, go to the Tools -> Options menu and select the General tab. Under Settings, uncheck the item R1C1 reference style. If you use explicit cell addresses in MLGetMatrix and later insert or delete rows or columns, or move or copy the function to another cell, edit the argument to correct the addresses. Excel Link does not automatically adjust cell addresses in MLGetMatrix. Enter (type) Excel Link functions directly in worksheet cells. Do not use the Excel Function Wizard; it generates unpredictable results.

Macros
To create macros that use Excel Link functions, you must first configure Excel to reference the functions from the Excel Link add-in. From the Visual Basic environment pull down the Insert menu and select Module. When the Module page opens, pull down the Tools menu and select References. In the References window, select the box for excllink.xla and click OK. You may have to use Browse to find the excllink.xla file. If you use MLGetMatrix in a macro subroutine, enter MatlabRequest on the line after MLGetMatrix. MatlabRequest initializes internal Excel Link variables and enables MLGetMatrix to function in a subroutine. For example:
Sub Get_RangeA() MLGetMatrix "A", "RangeA" MatlabRequest End Sub

Do not include MatlabRequest in a macro function unless the macro function is called from a subroutine.

Data Types
Excel Link handles only MATLAB two-dimensional numeric arrays, one-dimensional character arrays (strings), and two-dimensional cell arrays. It does not work with MATLAB multidimensional arrays and structures.

Dates
Default Excel date numbers start from January 1, 1900, while MATLAB date numbers start from January 1, 0000. Thus May 15, 1996 is 35200 in Excel

1-11

Getting Started

and 729160 in MATLAB, a difference of 693960. If you use date numbers in MATLAB calculations, apply the 693960 constant: add it to Excel date numbers going into MATLAB, or subtract it from MATLAB date numbers coming into Excel. If you use the optional Excel 1904 date system, the constant is 695422.

Saved Worksheets
When you open an Excel worksheet that contains Excel Link functions, Excel tries to execute the functions from the bottom up and right to left, thus possibly generating cell error messages (#COMMAND!, #NONEXIST!, etc.). Such behavior is usual for Excel. Simply ignore the messages, close any MATLAB figure windows, and reexecute the cell functions one at a time in the correct order by pressing F2, and then Enter. If you save an Excel worksheet containing Excel Link functions and later open it under a different computer environment where the excllink.xla add-in is in a different location, Excel may display a message box.

Click No. Then pull down the Edit menu and select Links. In the Links window, click Change Source. In the Change Links window, find and select excllink.xla under <matlab>/toolbox/exlink and click OK. Excel executes each function as it changes its link. You may see MATLAB figure windows and hear error beeps as the links change and functions execute; ignore them. Back in the Links window, click OK. The worksheet now correctly connects to the Excel Link add-in. Or, instead of using the Edit Links menu, you can manually edit the link location in each affected worksheet cell to show the correct location of excllink.xla.

1-12

Tips and Reminders

Information for International Users


This document uses Excel with an English (United States) Windows regional setting for illustrative purposes. If you use Excel Link with a non-English (United States) Windows desktop environment, certain syntactical elements may not work as illustrated. For example, you may have to replace the comma (,) delimiter within the Excel Link commands with a semicolon (;) or other operator. Please consult your Windows documentation to determine which regional setting differences exist among various international versions.

1-13

Getting Started

1-14

2
Using Excel Link

Example 1: Regression and Curve Fitting (p. 2-3) Example 2: Interpolating Data (p. 2-9)

Data regression and curve fitting. Uses an Excel worksheet to organize and display the original data and the interpolated output data.

Example 3: Pricing a Stock Option with Uses the binomial model to price an option. the Binomial Model (p. 2-13) Example 4: Calculating and Plotting the Efficient Frontier of Financial Portfolios (p. 2-16) Example 5: Bond Cash Flow and Time Mapping (p. 2-20) Analyzes three portfolios, using rates of return for six time periods. Computes a set of cash flow amounts and dates given a portfolio of five bonds.

Using Excel Link

This section shows how Microsoft Excel, Excel Link, and MATLAB work together to solve real-world problems. These examples ship with Excel Link in the file ExliSamp.xls, which is installed in <matlab>/toolbox/exlink/. Start Excel, Excel Link, and MATLAB. Open and try executing the examples.

Note Examples 1 and 2 use only basic MATLAB functions. Examples 3, 4, and 5 use functions in the optional MATLAB Financial Toolbox. The Financial Toolbox in turn requires the Statistics and Optimization Toolboxes.

2-2

Example 1: Regression and Curve Fitting

Example 1: Regression and Curve Fitting


Regression techniques and curve fitting attempt to find functions that describe the relationship among variables. In effect, they attempt to build mathematical models of a data set. MATLAB provides many powerful yet easy-to-use matrix operators and functions to simplify the task. This example does both data regression and curve fitting. It also executes the same example in a worksheet version and a macro version. The example uses Excel worksheets to organize and display the data. Excel Link functions copy the data to MATLAB and execute MATLAB computational and graphic functions. The macro version also returns output data to an Excel worksheet.

Worksheet Version
To try the worksheet-only version of this example, click the Sheet1 tab on ExliSamp.xls.

2-3

Using Excel Link

The worksheet contains one named range: A4:C28 is named DATA and contains the sample data set:
1 Make E5 the active cell. Press F2, then Enter to execute the Excel Link

function that copies the sample data set to MATLAB. The data set contains 25 observations of three variables. There is a strong linear dependence among the observations; in fact, they are close to being scalar multiples of each other.
2 Move to cell E8 and press F2, then Enter. Repeat with cells E9 and E10.

These Excel Link functions tell MATLAB to regress the third column of data on the other two columns. They create a single vector y containing the third-column data, and a new three-column matrix A consisting of a column of ones followed by the rest of the data.
3 Execute the function in cell E13. This function computes the regression

coefficients by using the MATLAB backslash operation to solve the (overdetermined) system of linear equations, A*beta = y.
4 Execute the function in cell E16. MATLAB matrix-vector multiplication

produces the regressed result (fit).


5 Execute the functions in cells E19, E20, and E21. These functions compare

the original data with fit; sort the data in increasing order and apply the same permutation to fit; and create a scalar for the number of observations.
6 Execute the functions in cells E24 and E25. Often it is useful to fit a

polynomial equation to data. To do so, you would ordinarily have to set up a system of simultaneous linear equations and solve for the coefficients. The MATLAB polyfit function automates this procedure, in this case for a fifth-degree polynomial. The polyval function then evaluates the resulting polynomial at each data point to check the goodness of fit (newfit).
7 Finally, execute the function in cell E28. The MATLAB plot function graphs

the original data (blue circles), the regressed result fit (dashed red line), and the polynomial result (solid green line); and adds a legend.

2-4

Example 1: Regression and Curve Fitting

Since the data is closely correlated but not exactly linearly dependent, the fit curve (dashed line) shows a close, but not an exact, fit. The fifth-degree polynomial curve, newfit, represents a more accurate mathematical model for the data. When you have finished this version of the example, close the figure window.

2-5

Using Excel Link

Macro Version
To try the macro-and-worksheet version of this example, click the Sheet2 tab on ExliSamp.xls.
.

Make cell A4 the active cell, but do not execute it yet. Cell A4 calls the macro CurveFit, which you can examine from the Visual Basic environment.

2-6

Example 1: Regression and Curve Fitting

While this module is open, pull down the Tools menu and select References. In the References window, make sure there is a check in the box for excllink.xla. If not, check the box and click OK. You may have to use Browse to find the excllink.xla file. Back in cell A4 of Sheet2, press F2, then Enter to execute the CurveFit macro. The macro executes the same functions as in Step 1 through Step 7 of the worksheet version (in a slightly different order), including plotting the graph. Plus, it copies the original data y (sorted), the corresponding regressed data fit, and the polynomial data newfit, to the worksheet. (The last three MLGetMatrix functions in the CurveFit macro copy data to the Excel worksheet.)

2-7

Using Excel Link

When you have finished the example, close the figure window.

2-8

Example 2: Interpolating Data

Example 2: Interpolating Data


Interpolation is a process for estimating values that lie between known data points. It is important for applications such as signal and image processing and data visualization. MATLAB provides a number of interpolation functions that let you balance the smoothness of data fit with execution speed and efficient memory use. This example uses a two-dimensional data-gridding interpolation function on thermodynamic data, where volume has been measured for time and temperature values. It finds the volume values underlying the two-dimensional time-temperature function for a new set of time and temperature coordinates. The example uses an Excel worksheet to organize and display the original data and the interpolated output data. Excel Link functions copy the data to and from MATLAB, execute the MATLAB interpolation function, and invoke MATLAB graphics to display the interpolated data in a three-dimensional color surface. To try this example, click the Sheet3 tab on ExliSamp.xls.

2-9

Using Excel Link

The worksheet contains the measured thermodynamic data in cells A5:A29, B5:B29, and C5:C29. The time and temperature values for interpolation are in cells E7:E30 and F6:T6 respectively:
1 Make A33 the active cell. Press F2, then Enter to execute the Excel Link

function that passes the Time, Temp, and Volume labels to MATLAB.
2 Make A34 the active cell. Press F2, then Enter to execute the Excel Link

function that copies the original time data to MATLAB. Move to cell A35 and

2-10

Example 2: Interpolating Data

execute the function to copy the original temperature data. Execute the function in cell A36 to copy the original volume data.
3 Move to cell A39 and press F2, then Enter to copy the interpolation time

values to MATLAB. Execute the function in cell A40 to copy the interpolation temperature values.
4 Execute the function in cell A43. griddata is the MATLAB two-dimensional

interpolation function that generates the interpolated volume data using the inverse distance method.
5 Execute the functions in cells A46 and A47 to transpose the interpolated

volume data and copy it to the Excel worksheet. The data fills cells F7:T30, which are enclosed in a border.

6 Execute the function in cell A50. MATLAB plots and labels the interpolated

data on a three-dimensional color surface, with the color proportional to the interpolated volume data.

2-11

Using Excel Link

When you have finished with the example, close the figure window.

2-12

Example 3: Pricing a Stock Option with the Binomial Model

Example 3: Pricing a Stock Option with the Binomial Model


The MATLAB Financial Toolbox provides several functions that compute prices, sensitivities, and profits for portfolios of options or other equity derivatives. This example uses the binomial model to price an option. The binomial model assumes that the probability of each possible price over time follows a binomial distribution; that is, that prices can move to only two values, one up and one down, over any short time period. Plotting the two values, and then the subsequent two values each, and then the subsequent two values each, and so on, over time, is known as building a binomial tree. This example uses the Excel worksheet to organize and display input and output data. Excel Link functions copy data to a MATLAB matrix, calculate the prices, and return data to the worksheet.

Note This example requires use of the optional MATLAB Financial Toolbox.

Click the Sheet4 tab on ExliSamp.xls to try this example.

2-13

Using Excel Link

The worksheet contains three named ranges: B4:B10 named bindata B15 named asset_tree B23 named value_tree Also, two cells in bindata actually contain formulas: B7 contains =5/12 B8 contains =1/12 Make D5 the active cell. Press F2, then Enter to execute the Excel Link function that copies the asset data to MATLAB. Move to D8 and execute the function that computes the binomial prices, then execute the functions in D11 and D12 to copy the price data to Excel. The worksheet looks like this.

2-14

Example 3: Pricing a Stock Option with the Binomial Model

Read the asset price tree this way: Period 1 shows the up and down prices, Period 2 shows the up-up, up-down, and down-down prices, Period 3 shows the up-up-up, up-up, down-down, and down-down-down prices, and so on. Ignore the zeros. The option value tree gives the associated option value for each node in the price tree. Because this is a put, the option value is zero for prices significantly above the exercise price. Ignore the zeros that correspond to a zero in the price tree. Try changing the data in B4:B10 and reexecuting the Excel Link functions. Note, however, that if you increase the time to maturity (B7) or change the time increment (B8), you may need to enlarge the output tree areas.

2-15

Using Excel Link

Example 4: Calculating and Plotting the Efficient Frontier of Financial Portfolios


MATLAB and the Financial Toolbox provide functions that compute and graph risks, variances, rates of return, and the efficient frontier of portfolios. Efficient portfolios have the lowest aggregate variance, or risk, for a given return. Excel and Excel Link let you set up data, execute financial functions and MATLAB graphics, and display numeric results. This example analyzes three portfolios, using rates of return for six time periods. In actual practice, these functions can analyze many portfolios over many time periods, limited only by the amount of computer memory available.

Note This example requires use of the optional MATLAB Financial Toolbox.

Click the Sheet5 tab on ExliSamp.xls to try this example.

2-16

Example 4: Calculating and Plotting the Efficient Frontier of Financial Portfolios

Make A15 the active cell. Press F2, then Enter to execute the Excel Link function that transfers the labels describing the outputs to be computed by MATLAB. Then make A16 the active cell to copy the actual portfolio return data to MATLAB. Execute the functions in A19 and A20 to compute the MATLAB Financial Toolbox efficient frontier function for 20 points along the frontier. Execute the Excel Link functions in A23, A24, and A25 to copy the output data to Excel.

2-17

Using Excel Link

The worksheet looks like this.

The data describes the efficient frontier for these three portfolios: that set of points representing the highest rate of return (ROR) for a given risk. For each of the 20 points along the frontier, the weighted investment in each portfolio (Weights) would achieve that rate of return. Now move to A28 and press F2, then Enter to execute the Financial Toolbox function that plots the efficient frontier for the same portfolio data.

2-18

Example 4: Calculating and Plotting the Efficient Frontier of Financial Portfolios

MATLAB displays a figure.

The light blue line shows the efficient frontier. Note the change in slope above a 6.8% return because the Corporate Bond portfolio no longer contributes to the efficient frontier. To try different data, close the figure window and change the data in cells B4:D9. Then reexecute all the Excel Link functions. The worksheet then shows the new frontier data, and MATLAB displays a new efficient frontier graph.

2-19

Using Excel Link

Example 5: Bond Cash Flow and Time Mapping


Example 5 illustrates the use of the MATLAB Financial Toolbox and Excel Link to compute a set of cash flow amounts and dates given a portfolio of five bonds whose maturity dates and coupon rates are known. Click the Sheet6 tab on ExliSamp.xls to try this example.

Make A18 the active cell. Press F2, then Enter to execute the Excel Link function that transfers the column vector Maturity to MATLAB. Make A19 the active cell to transfer the column vector Coupon Rate to MATLAB. Make A20 the active cell to transfer the settlement date to MATLAB. Execute the functions in cells A23 and A24 to use the Financial Toolbox to compute cash flow

2-20

Example 5: Bond Cash Flow and Time Mapping

amounts and dates. Now execute the functions in cells A27 through A29 to transform the dates into string form contained in a cell array. Execute the functions in cells A32 through A34 to transfer the data to Excel.

Finally, execute the function in cell A37 to display a MATLAB plot of the cash flows for each portfolio item.

2-21

Using Excel Link

2-22

3
Function Reference
Functions - By Category (p. 3-2) Functions - Alphabetical List (p. 3-5) Functions organized by topic Functions organized alphabetically

Function Reference

Functions - By Category
Link Management Functions
matlabinit MLAutoStart MLClose MLOpen

Initialize Excel Link and start MATLAB process. Automatically start MATLAB process. Terminate MATLAB process. Start MATLAB process.

You can invoke any link management function except matlabinit as a worksheet cell formula or in a macro. You invoke matlabinit from the Excel Tools Macro menu or in a macro subroutine.

3-2

Functions - By Category

Data Management Functions


matlabfcn matlabsub MLAppendMatrix MLDeleteMatrix MLEvalString MLGetMatrix MLGetVar MLPutMatrix MLPutVar

Evaluate MATLAB command given Excel data. Evaluate MATLAB command given Excel data and designate output location. Create or append MATLAB matrix with data from Excel worksheet. Delete MATLAB matrix. Evaluate command in MATLAB. Write contents of MATLAB matrix in Excel worksheet. Write contents of MATLAB matrix in Excel VBA variable. Create or overwrite MATLAB matrix with data from Excel worksheet. Create or overwrite MATLAB matrix with data from Excel VBA variable.

You can invoke any data management function except MLGetVar and MLPutVar as a worksheet cell formula or in a macro. You can invoke MLGetVar and MLPutVar only in a macro.

3-3

Functions Alphabetical List


This section contains function reference pages listed alphabetically.

3-4

matlabfcn

Purpose Syntax

3matlabfcn

Evaluate MATLAB command given Excel data


Worksheet:
command inputs matlabfcn(command, inputs)

MATLAB command to evaluate. The MATLAB command must be written as "command" (in double quotes). Variable length input argument list passed to MATLAB command. Argument list may contain a range of worksheet cells that contain input data.

Description

Passes the command to MATLAB for evaluation given the function input data. The function returns a single value or string depending upon the MATLAB output. The result is returned to the calling worksheet cell. This function is intended for use as an Excel worksheet function.
matlabfcn("sum", B1:B10)

Examples

sums the data in the spreadsheet cells B1 through B10 returning the output to the active worksheet cell or Excel Visual Basic for Applications (VBA) output variable.
matlabfcn("plot", B1:B10, "x")

plots the data in worksheet cells B1 through B10 using x as the marker type.

See Also

matlabsub

3-5

matlabinit

Purpose Syntax

3matlabinit

Initialize Excel Link and start MATLAB process


matlabinit

Note To run matlabinit, pull down the Excel Tools menu and select Macro. In the Macro Name/Reference box, enter matlabinit and click Run. Or, include it in a macro subroutine. You cannot run matlabinit as a worksheet cell formula or in a macro function.

Description

Initializes Excel Link and starts MATLAB process. If Excel Link has already been initialized and MATLAB is running, subsequent invocations do nothing. Use matlabinit to start Excel Link and MATLAB manually when you have set MLAutoStart to "no". If MLAutoStart is set to "yes", matlabinit executes automatically.
MLAutoStart, MLOpen

See Also

3-6

matlabsub

Purpose Syntax

3matlabsub

Evaluate MATLAB command given Excel data and designate output location
Worksheet:
command edat matlabsub(command, edat, inputs)

MATLAB command to evaluate. The MATLAB command must be written as "command" (in double quotes). Worksheet location where the function writes the contents of var_name. "edat" (in quotes) directly specifies the location and it must be a cell address or a range name. edat (without quotes) is an indirect reference: the function evaluates the contents of edat to get the location. edat must be a worksheet cell address or range name. Variable length input argument list passed to MATLAB command. Argument list may contain a range of worksheet cells that contain input data.

inputs

Description

Passes the command to MATLAB for evaluation given the function input data. The function returns a single value or string depending upon the MATLAB output. This function is intended for use as an Excel worksheet function. To return an array of data to the Excel Visual Basic for Applications (VBA) workspace, see MLEvalString and MLGetVar.

Caution edat must not include the cell that contains the matlabsub function. In other words, be careful not to overwrite the function itself. Also make sure there is enough room in the worksheet to write the matrix contents. If there is insufficient room, the function generates a fatal error.

3-7

matlabsub

Examples

matlabsub("sum", "A1", B1:B10)

sums the data in worksheet cells B1 through B10, returning the output to cell A1.

See Also

matlabfcn

3-8

MLAppendMatrix

Purpose Syntax

3MLAppendMatrix

Create or append MATLAB matrix with data from Excel worksheet


Worksheet: Macro:
var_name MLAppendMatrix(var_name, mdat) MLAppendMatrix var_name, mdat

Name of MATLAB matrix to which to append data. "var_name" (in quotes) directly specifies the matrix name. var_name (without quotes) is an indirect reference: the function evaluates the contents of var_name to get the matrix name, and var_name must be a worksheet cell address or range name Location of data to append to var_name. mdat (no quotes). Must be a worksheet cell address or range name. If this argument is not initially an Excel Range data type and you call the function from a worksheet, Excel proceeds by performing the necessary type coercion. However, if you call MLAppendMatrix from within a VBA macro, and mdat is not an Excel Range data type, the call fails. Excel generates the error message ByRef Argument Type Mismatch.

mdat

Description

Appends data in mdat to MATLAB matrix var_name. Creates var_name if it does not exist. The function checks the dimensions of var_name and mdat to determine how to append mdat to var_name. If the dimensions allow appending mdat as either new rows or new columns, it appends mdat to var_name as new rows. The function returns an error if the dimensions do not match. mdat must contain either numeric data or string data. Data types cannot be combined within the range specified in mdat. Empty mdat cells become MATLAB matrix elements containing zero if the data is numeric and empty strings if the data is a string.
B is a 2-by-2 MATLAB matrix. MLAppendMatrix("B", A1:A2)

Examples

3-9

MLAppendMatrix

appends the data in cell range A1:A2 to the MATLAB matrix B. B is now a 2-by-3 matrix with the data from A1:A2 in the third column. A1 A2
B is a 2-by-2 MATLAB matrix. Cell C1 contains the label (string) B, and new_data is the name of the cell range A1:B2. MLAppendMatrix(C1, new_data)

appends the data in cell range A1:B2 to B. B is now a 4-by-2 matrix with the data from A1:B2 in the last two rows.

A1 A2

B1 B2

See Also

MLPutMatrix

3-10

MLAutoStart

Purpose Syntax

3MLAutoStart

Automatically start MATLAB process


Worksheet: Macro:
"yes" "no" MLAutoStart("yes") MLAutoStart("no") MLAutoStart "yes" MLAutoStart "no"

Automatically start Excel Link and MATLAB every time Excel starts (default). Cancel automatic startup of Excel Link and MATLAB. If Excel Link and MATLAB are running, it does not stop them.

Description

Sets automatic startup of Excel Link and MATLAB. When Excel Link is installed, the default is yes. A change of state takes effect the next time Excel is started.
MLAutoStart("no")

Examples

cancels automatic startup of Excel Link and MATLAB. The next time Excel starts, Excel Link and MATLAB will not start.

See Also

matlabinit, MLClose, MLOpen

3-11

MLClose

Purpose Syntax

3MLClose

Terminate MATLAB process


Worksheet: Macro:
MLClose() MLClose

Description

Terminates the MATLAB process, deletes all variables from the MATLAB workspace, and tells Excel that MATLAB is no longer running. If no MATLAB process is running, nothing happens.
MLOpen

See Also

3-12

MLDeleteMatrix

Purpose Syntax

3MLDeleteMatrix

Delete MATLAB matrix


Worksheet: Macro:
var_name MLDeleteMatrix(var_name) MLDeleteMatrix var_name

Name of MATLAB matrix to delete. "var_name" (in quotes) directly specifies the matrix name. var_name (without quotes) is an indirect reference: the function evaluates the contents of var_name to determine the matrix name, and var_name must be a worksheet cell address or range name.

Description Example

Deletes the named matrix from the MATLAB workspace.


MLDeleteMatrix("A")

deletes matrix A from the MATLAB workspace.

3-13

MLEvalString

Purpose Syntax

3MLEvalString

Evaluate command in MATLAB


Worksheet: Macro:
command MLEvalString(command) MLEvalString command

MATLAB command to evaluate. "command" (in quotes) directly specifies the command. command (without quotes) is an indirect reference: the function evaluates the contents of command to get the command, and command must be a worksheet cell address or range name.

Description Examples

Passes the command string to MATLAB for evaluation. The specified action alters only the MATLAB workspace. Nothing is done in the Excel workspace.
MLEvalString("b = b/2;plot(b)")

divides the MATLAB variable b by 2 and plots it. Only the MATLAB variable b is modified. To update data in the Excel worksheet, use MLGetMatrix.

See Also

MLGetMatrix

3-14

MLGetMatrix

Purpose Syntax

3MLGetMatrix

Write contents of MATLAB matrix in Excel worksheet


Worksheet: Macro:
var_name MLGetMatrix(var_name, edat) MLGetMatrix var_name, edat

Name of MATLAB matrix to access. "var_name" (in quotes) directly specifies the matrix name. var_name (without quotes) is an indirect reference: the function evaluates the contents of var_name to get the matrix name, and var_name must be a worksheet cell address or range name. var_name cannot be the MATLAB variable ans. Worksheet location where the function writes the contents of var_name. "edat" (in quotes) directly specifies the location and it must be a cell address or a range name. edat (without quotes) is an indirect reference: the function evaluates the contents of edat to get the location, and edat must be a worksheet cell address or range name.

edat

Description

Writes the contents of MATLAB matrix var_name in the Excel worksheet, beginning in the upper left cell specified by edat. If data already exists in the specified worksheet cells, it is overwritten. If the dimensions of the MATLAB matrix are larger than those of the specified cells, the data will overflow into additional rows and columns.

Caution edat must not include the cell that contains the MLGetMatrix function. In other words, be careful not to overwrite the function itself. Also make sure there is enough room in the worksheet to write the matrix contents. If there is insufficient room, the function generates a fatal error.

If edat is an explicit cell address and you later insert or delete rows or columns, or move or copy the function to another cell, edit edat to correct the address. Excel Link does not automatically adjust cell addresses in MLGetMatrix.

3-15

MLGetMatrix

If worksheet calculation mode is automatic, MLGetMatrix executes when you enter the formula in a cell. If worksheet calculation mode is manual, enter the MLGetMatrix function in a cell, then press F9 to execute it. However, pressing F9 in this situation may also re-execute other worksheet functions and generate unpredictable results. If you use MLGetMatrix in a macro subroutine, enter MatlabRequest on the line after the MLGetMatrix. MatlabRequest initializes internal Excel Link variables and enables MLGetMatrix to function in a subroutine. Do not include MatlabRequest in a macro function unless the function is called from a subroutine.

Examples

MLGetMatrix("bonds", "Sheet2!C10")

writes the contents of the MATLAB matrix bonds starting in cell C10 of Sheet2. If bonds is a 4-by-3 matrix, data fills cells C10..E13.
MLGetMatrix(B12, B13)

accesses the MATLAB matrix named as a string in worksheet cell B12 and writes the contents of the matrix in the worksheet starting at the location named as a string in worksheet cell B13.
Sub Get_RangeA() MLGetMatrix "A", "RangeA" MatlabRequest End Sub

writes the contents of MATLAB matrix A in the worksheet starting at the cell named RangeA.

See Also

MLAppendMatrix, MLPutMatrix

3-16

MLGetVar

Purpose Syntax

3MLGetVar

Write contents of MATLAB matrix in Excel VBA variable


MLGetVar ML_var_name, VBA_var_name ML_var_name

Name of MATLAB matrix to access. "ML_var_name" (in quotes) directly specifies the matrix name. ML_var_name (without quotes) is an indirect reference: the function evaluates the contents of ML_var_name to get the matrix name, and ML_var_name must be a VBA variable containing the matrix name as a string. var_name cannot be the MATLAB variable ans. Name of VBA variable where the function writes the contents of ML_var_name. Use VBA_var_name without quotes.

VBA_var_name

Description

Writes the contents of MATLAB matrix ML_var_name in the Excel Visual Basic for Applications (VBA) variable VBA_var_name. Creates VBA_var_name if it does not exist. Replaces existing data in VBA_var_name. Use MLGetVar only in a macro subroutine, not in a macro function or in a subroutine called by a function.
Sub Fetch() MLGetVar "J", DataJ End Sub

Examples

writes the contents of MATLAB matrix J in the VBA variable named DataJ.

See Also

MLPutVar

3-17

MLOpen

Purpose Syntax

3MLOpen

Start MATLAB process


Worksheet: Macro:
MLOpen() MLOpen

Description

Starts MATLAB process. If a MATLAB process has already been started, subsequent calls to MLOpen do nothing. Use MLOpen to restart MATLAB after you have stopped it with MLClose in a given Excel session.

Note We recommend using matlabinit rather than MLOpen, since matlabinit starts MATLAB and initializes Excel Link.

Examples

MLOpen()

starts the MATLAB process.

See Also

matlabinit, MLClose

3-18

MLPutMatrix

Purpose Syntax

3MLPutMatrix

Create or overwrite MATLAB matrix with data from Excel worksheet


Worksheet: Macro:
var_name MLPutMatrix(var_name, mdat) MLPutMatrix var_name, mdat

Name of MATLAB matrix to create or overwrite. "var_name" (in quotes) directly specifies the matrix name. var_name (without quotes) is an indirect reference: the function evaluates the contents of var_name to get the matrix name, and var_name must be a worksheet cell address or range name. Location of data to copy into var_name. mdat (no quotes). Must be a worksheet cell address or range name.

mdat

Description

Creates or overwrites matrix var_name in MATLAB workspace with specified data in mdat. Creates var_name if it does not exist. If var_name already exists, this function replaces the contents with mdat. Empty numeric data cells within the range of mdat become numeric zeros within the MATLAB matrix identified by var_name. If any element of mdat contains string data, mdat is exported as a MATLAB cell array. Empty string elements within the range of mdat become NaNs within the MATLAB cell array. To use MLPutMatrix in a subroutine, you must indicate the source of the worksheet data using the Excel macro Range. For example:
Sub test() MLPutMatrix "a", Range("A1:A3") End Sub

If you have a named range in your worksheet, you can use the name instead of actually specifying the range. For example:
Sub test() MLPutMatrix "a", Range("temp") End Sub

where temp is a named range in your worksheet.

3-19

MLPutMatrix

Examples

MLPutMatrix("A", A1:C3)

creates or overwrites matrix A in the MATLAB workspace with the data in the worksheet range A1:C3.

See Also

MLAppendMatrix, MLGetMatrix

3-20

MLPutVar

Purpose Syntax

3MLPutVar

Create or overwrite MATLAB matrix with data from Excel VBA variable
MLPutVar ML_var_name, VBA_var_name ML_var_name

Name of MATLAB matrix to create or overwrite. "ML_var_name" (in quotes) directly specifies the matrix name. ML_var_name (without quotes) is an indirect reference: the function evaluates the contents of ML_var_name to get the matrix name, and ML_var_name must be a VBA variable containing the matrix name as a string. Name of VBA variable whose contents are written to ML_var_name. Use VBA_var_name without quotes.

VBA_var_name

Description

Creates or overwrites matrix ML_var_name in MATLAB workspace with data in VBA_var_name. Creates ML_var_name if it does not exist. If ML_var_name already exists, this function replaces the contents with data from VBA_var_name. Use MLPutVar only in a macro subroutine, not in a macro function or in a subroutine called by a function. Empty numeric data cells within VBA_var_name become numeric zeros within the MATLAB matrix identified by ML_var_name. If any element of VBA_var_name contains string data, VBA_var_name is exported as a MATLAB cell array. Empty string elements within VBA_var_name become NaNs within the MATLAB cell array.

Examples

Sub Put() MLPutVar "K", DataK End Sub

creates or overwrites MATLAB matrix K with the data in the Excel Visual Basic for Applications (VBA) variable DataK.

See Also

MLGetVar

3-21

MLPutVar

3-22

A
Error Messages and Troubleshooting

Excel Cell Error Messages (p. A-2) Excel Error Message Boxes (p. A-5) Audible Error Signals (p. A-7) Data Errors (p. A-8)

Error messages displayed in a worksheet cell. Error messages displayed in an Excel error message box. Audible error signals while passing data to MATLAB. Undesirable data characteristics.

Error Messages and Troubleshooting

Excel Cell Error Messages


Excel may display one of these error messages in a worksheet cell.

Table A-1: Excel Cell Error Messages Excel Cell Error Message #COLS>256 Meaning Solution

Your MATLAB variable exceeds the Excel limit of 256 columns. MATLAB does not recognize the command in an MLEvalString function. The command may be misspelled. You used
MLAppendMatrix and the

This is a limitation in Excel. Try the computation with a variable containing fewer columns. Verify the spelling of the MATLAB command. Correct typing errors.

#COMMAND!

#DIMENSION!

dimensions of the appended data do not match the dimensions of the matrix you want to append.
#INVALIDNAME!

Verify the matrix dimensions and the appended data dimensions, and correct the argument. See MLAppendMatrix in Chapter 3, Function Reference. Make sure to use legal MATLAB variable names. MATLAB variable names must start with a letter followed by up to 30 letters, digits, or underscores. See Data Types on page 1-11 for a list of supported MATLAB data types.

You entered an illegal variable name.

#INVALIDTYPE!

You have specified an illegal MATLAB data type with MLGetVar or MLGetMatrix.

A-2

Excel Cell Error Messages

Table A-1: Excel Cell Error Messages (Continued) Excel Cell Error Message #MATLAB? Meaning Solution

You used an Excel Link function and MATLAB is not running. Excel doesnt recognize the function name. The excllink.xla add-in is not loaded, or the function name may be misspelled. You referenced a nonexistent matrix in an MLGetMatrix or
MLDeleteMatrix

Start Excel Link and MATLAB. See Starting Excel Link on page 1-5. Be sure the excllink.xla add-in is loaded. See Configuring Excel to Work with Excel Link on page 1-3. Check the spelling of the function name. Correct typing errors. Verify the spelling of the MATLAB matrix. Use the MATLAB whos command to display existing matrices. Correct typing errors. This is a limitation in Excel. Try the computation with a variable containing fewer rows. Verify and correct the function syntax. See Chapter 3, Function Reference for function syntax.

#NAME?

#NONEXIST!

function. The matrix name may be misspelled.


#ROWS>65536

Your MATLAB variable exceeds the Excel limit of 65536 rows. You entered an Excel Link function with incorrect syntax; for example, the double quotes (") may be missing, or you used single quotes (') instead of double quotes.

#SYNTAX?

A-3

Error Messages and Troubleshooting

Table A-1: Excel Cell Error Messages (Continued) Excel Cell Error Message #VALUE! Meaning Solution

An argument is missing from a function, or a function argument is the wrong type. A macro subroutine uses MLGetMatrix followed by MatlabRequest, which is correct standard usage. A macro function calls that subroutine, and you execute that function from a worksheet cell. The function works correctly, but this message appears in the cell.

Supply the correct number of function arguments, of the correct type. Since the function works correctly, you may ignore the message. Or, in this special case, remove MatlabRequest from the subroutine.

#VALUE!

Note When you open an Excel worksheet that contains Excel Link functions, Excel tries to execute the functions from the bottom up and right to left, thus possibly generating cell error messages (#COMMAND!, #NONEXIST!, etc.). Such behavior is usual for Excel. Simply ignore the messages, close any MATLAB figure windows, and reexecute the cell functions one at a time in the correct order by pressing F2, then Enter.

A-4

Excel Error Message Boxes

Excel Error Message Boxes


Excel may display one of these error message boxes.

Table A-2: Excel Error Message Boxes Excel Error Message Box Meaning Solution

You entered a formula incorrectly. Common errors include a space between the function name and the left parenthesis; or missing, extra, or mismatched parentheses. You tried to execute a macro and the location of excllink.xla is incorrect.

Check entry and correct typing errors.

Click OK. The References window opens. Remove the check from MISSING: excllink.xla. Find excllink.xla in its correct location, check its box in the References window, and click OK.

A-5

Error Messages and Troubleshooting

Table A-2: Excel Error Message Boxes (Continued) Excel Error Message Box Meaning Solution

You used MLGetMatrix and the matrix is larger than the space available in the worksheet. This error destabilizes Excel Link and changes worksheet calculation mode to manual. The license passcode that you entered was invalid.

Click OK. Reset worksheet calculation mode to automatic and save your worksheet (if desired). Close Excel and MATLAB. Restart Excel, Excel Link, and MATLAB. Check that you entered the license passcode properly. If you used a proper passcode and you are still unable to start Excel Link, contact your MathWorks representative.

A-6

Audible Error Signals

Audible Error Signals


Audible error signals while passing data to MATLAB with MLPutMatrix or MLAppendMatrix usually mean you have insufficient computer memory to carry out the operation. Close other applications or clear unnecessary variables from the MATLAB workspace and try again. If the error signal reoccurs, you probably have insufficient physical memory in your computer for this operation.

A-7

Error Messages and Troubleshooting

Data Errors
Data in the MATLAB or Excel workspaces may exhibit these undesired characteristics.
Table A-3: Data Errors Data Error Cause Solution

MATLAB matrix cells contain zeros (0). MATLAB matrix is a 1-by-1 zero matrix.

Corresponding Excel worksheet cells are empty. You used quotes around the data-location argument in MLPutMatrix or MLAppendMatrix. You referenced a nonexistent VBA variable in MLPutVar. You referenced a nonexistent MATLAB variable in MLGetVar.

Excel worksheet cells must contain only numeric or string data. Correct the syntax to remove quotes.

MATLAB matrix is empty ([]).

Correct the macro; you may have typed the variable name incorrectly. Correct the macro; you may have typed the variable name incorrectly.

VBA matrix is empty.

A-8

B
Installed Files

Files and Directories (p. B-2)

Locations of files and directories created by Excel Link installation.

Installed Files

Files and Directories


The Excel Link installation program creates the subdirectory exlink under <matlab>/toolbox/. This directory contains the files excllink.xla: Excel Link add-in ExliSamp.xls: Excel Link samples described in this manual Installation also creates an Excel Link initialization file, exlink.ini, in the appropriate Windows directory (for example, C:\Winnt). For all operating systems, the C:\MATLAB\bin directory should be on your system path. On Windows 2000 add the C:\Winnt\system and C:\Winnt\system32 directories to your path. Excel Link uses Kernel32.dll, which should already be in the appropriate Windows system directory (for example, C:\Winnt\system32).

B-2

Index
Symbols
# A-2, A-3
/automation option 1-5

E
efficient frontier example 2-16 empty matrix A-8 error message boxes A-5 error messages A-2 examples cash flow 2-20 efficient frontier 2-16 interpolating data 2-9 regression and curve fitting 2-3 stock option 2-13 Excel error message boxes A-5 Excel Link installing 1-3 starting 1-5 stopping 1-3, 1-5 excllink.xla B-2 excllink.xla add-in A-5 exlink subdirectory B-2 exlink.ini file B-2 ExliSamp.xls file location B-2 purpose 2-2

Numerics
1904 date system 1-12

A
add-in, Excel Link 1-4, A-3 audible error signals A-7

B
beeps A-7 binomial tree 2-13

C
calculation mode A-6 cash flow example 2-20 cell error messages A-2 COLS error A-2 COMMAND error A-2 computer memory errors A-7 curve fitting example 2-3

F
function names 1-9

D
data errors A-8 data interpolation example 2-9 data types 1-11 data-location argument A-7, A-8 dates 1-11 DIMENSION error A-2 double quotes A-3

I
initialization file B-2 interpolating data 2-9 INVALIDNAME error A-2 INVALIDTYPE error A-2

Index-1

Index

K
Kernel32.dll B-2

R
regression and curve fitting 2-3 requirements 1-3 ROWS error A-3

L
license passcode A-6 link management functions 1-7

S
signals error A-7 single quotes A-3 stock option pricing example 2-13 SYNTAX error A-3 system path files on B-2

M
macros 1-11
MATLAB error A-3 matlabfcn 3-5 matlabinit 3-6 matlabsub 3-7

matrix dimensions A-2 MLAppendMatrix 3-9 MLAutoStart 3-11 MLClose 3-12 MLDeleteMatrix 3-13 MLEvalString 3-14 MLGetMatrix 3-15 MLGetVar 3-17 MLOpen 3-18 MLPutMatrix 3-19 MLPutVar 3-21

T
troubleshooting error messages A-2

V
VALUE error A-4 variable names 1-9

W
worksheetformulas 1-9 worksheets 1-10 saved 1-12

N
NAME error A-3 NONEXIST error A-3

Z
zero matrix A-8 zero matrix cells A-8

nonexistent variable A-8

P
passcode license A-6

Index-2

You might also like