Create or overwrite MATLAB matrix with data from Microsoft Excel worksheet


= MLPutMatrix(var_name, mdat)
MLPutMatrix var_name, mdat
out = MLPutMatrix(var_name,mdat)


= MLPutMatrix(var_name, mdat) creates or overwrites matrix var_name in MATLAB® workspace with specified data in mdat. Creates var_name if it does not exist. Use this syntax when working directly in a worksheet.

MLPutMatrix var_name, mdat creates or overwrites matrix var_name in MATLAB workspace with specified data in mdat. Use this syntax in a VBA macro.

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

Input Arguments


Name of MATLAB matrix to create or overwrite.

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.


Location of data to copy into var_name.

mdat must be a worksheet cell address or range name. Do not enclose it in quotes.

Output Arguments


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


collapse all

Create or overwrite matrix A in the MATLAB workspace with the data in the worksheet range A1:C3:

MLPutMatrix "A", Range("A1:C3")
  1. In the Excel® worksheet, select the columns and/or rows you want to export to the MATLAB workspace.

  2. Click the putmatrix button on the Spreadsheet Link™ toolbar. A window appears that prompts you to specify the name of the MATLAB variable in which you want to store your data.

  3. Enter newmatrix for the MATLAB variable name.

  4. Click OK.

Now you can manipulate newmatrix in the Command Window.

newmatrix =

     1     2     3
     4     5     6


  • If var_name 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.

  • When using MLPutMatrix in a subroutine, indicate the source of the worksheet data using the Microsoft® 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 specify the name instead of the range; for example:

    Sub test() 
      MLPutMatrix "a", Range("temp") 
    End Sub

    where temp is a named range in your worksheet.

Introduced before R2006a