Skip to Main Content Skip to Search
Product Documentation

MLGetMatrix - Write contents of MATLAB matrix to Microsoft Excel worksheet

Syntax

MLGetMatrix(var_name,edat)
MLGetMatrix var_name, edat
out = MLGetMatrix(var_name,edat)

Description

MLGetMatrix(var_name,edat) writes the contents of MATLAB matrix var_name in the Excel worksheet, beginning in the upper-left cell specified by edat. Use this syntax when working directly in a worksheet.

MLGetMatrix var_name, edat writes the contents of MATLAB matrix var_name in the Excel worksheet, beginning in the upper-left cell specified by edat. Use this syntax in a VBA macro.

out = MLGetMatrix(var_name,edat) lets you catch errors when executing MLGetMatrix in a VBA macro. If MLGetMatrix fails, then out is a string containing error code. Otherwise, out is 0.

Tips

Input Arguments

var_name

Name of MATLAB matrix to access.

var_name in quotes directly specifies the matrix name. var_name without quotes specifies a worksheet cell address (or range name) that contains the matrix name. Do not use the MATLAB variable ans as var_name.

edat

Worksheet location where the function writes the contents of var_name.

edat in quotes directly specifies the location. edat without quotes specifies a worksheet cell address (or range name) that contains a reference to the location. In both cases, edat must be a cell address or a range name.

Output Arguments

out

0 if the command succeeded. Otherwise, a string containing error code.

Examples

Specify the Matrix Name and Location Directly

Write the contents of the MATLAB matrix bonds starting in cell C10 of Sheet2. If bonds is a 4-by-3 matrix, fill cells C10..E13 with data:

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

Specify the Matrix Name and Location Indirectly

Access the MATLAB matrix named by the string in worksheet cell B12. Write the contents of the matrix to the worksheet starting at the location named by the string in worksheet cell B13:

MLGetMatrix(B12, B13)

Use MLGetMatrix in a VBA Macro

Write the contents of MATLAB matrix A to the worksheet, starting at the cell named by RangeA:

Sub Get_RangeA()
MLGetMatrix "A", "RangeA"
MatlabRequest
End Sub

Use the Address Property of the Range Object to Specify Location

In a macro, use the Address property of the range object returned by the VBA Cells function to specify where to write the data:

Sub Get_Variable()
MLGetMatrix "X", Cells(3, 2).Address
MatlabRequest
End Sub

Catch Errors in a VBA Macro

Use this function to get the variable A from MATLAB and to test if the command succeeded:

Sub myfun()
    Dim out As Variant
    
    out = MLGetMatrix("A", "A1")
    
    If out <> 0 Then
        MsgBox out
    End If
    MatlabRequest
End Sub

If MLGetMatrix fails, myfun displays a message box with the error code.

See Also

MLAppendMatrix | MLPutMatrix

  


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