Main Content

Pass MATLAB Data to .NET Functions

When you call a .NET method or function from MATLAB®, MATLAB converts arguments into .NET types. MATLAB performs this conversion on each passed argument, except for arguments that are already .NET objects.

For information about using .NET data in MATLAB instead, see Handle Data Returned from .NET Objects.

Pass MATLAB Scalar Data to .NET

This table shows MATLAB types for input data and the corresponding possible .NET types. MATLAB chooses a .NET type depending on the argument type that the .NET function expects. To view a .NET function signature including expected argument types, use the methodsview function.

MATLAB Type

Possible .NET Types
(listed from closest match to least close match)

Additional Information

logical
  • System.Boolean

  • System.Byte

  • System.SByte

  • System.Int16

  • System.UInt16

  • System.Int32

  • System.UInt32

  • System.Int64

  • System.UInt64

  • System.Single

  • System.Double

  • System.Object

-

double
  • System.Double

  • System.Single

  • System.Decimal

  • System.Int64

  • System.UInt64

  • System.Int32

  • System.Int32

  • System.Int16

  • System.UInt16

  • System.SByte

  • System.Byte

  • System.Object

-

single
  • System.Single

  • System.Double

  • System.Decimal

  • System.Object

-

int8
  • System.SByte

  • System.Int16

  • System.Int32

  • System.Int64

  • System.Single

  • System.Double

  • System.Object

-

uint8
  • System.Byte

  • System.UInt16

  • System.UInt32

  • System.UInt64

  • System.Single

  • System.Double

  • System.Object

-

int16
  • System.Int16

  • System.Int32

  • System.Int64

  • System.Single

  • System.Double

  • System.Object

-

uint16
  • System.UInt16

  • System.UInt32

  • System.UInt64

  • System.Single

  • System.Double

  • System.Object

-

int32
  • System.Int32

  • System.Int64

  • System.Single

  • System.Double

  • System.Object

-

uint32
  • System.UInt32

  • System.UInt64

  • System.Single

  • System.Double

  • System.Object

-

int64
  • System.Int64

  • System.Double

  • System.Object

-

uint64
  • System.UInt64

  • System.Double

  • System.Object

-

empty double ([])

System.Nullable

MATLAB converts empty double values ([]) to .NET null for reference type arguments.

char
  • System.Char

  • System.String

  • System.Object

  • MATLAB automatically converts a char array to a .NET System.String object.

  • To pass an array of char arrays, create a cell array.

string
  • System.String

  • System.Object

MATLAB automatically converts a string array to a .NET System.String[] array, where each string scalar is a System.String object.

<missing> value in string

null

-

string.empty

System.String[] with a size of 0

-

dictionary
  • System.Collections.Dictionary<,>

  • System.Collections.IDictionary<,>

  • System.Collections.IReadOnlyDictionary<,>

  • Collections.IDictionary

  • .NET objects can be keys or values in a dictionary

  • For the generic types (containing <,>), the keys and values must match the destination type. MATLAB does not convert the keys or values. For example, if a .NET function accepts Dictionary<int,int>, then you must pass a MATLAB dictionary with uint32 keys and values.

  • MATLAB converts dictionaries with entries of type cell to .NET dictionaries with entries of type System.Object.

  • To explicitly convert a MATLAB dictionary to a generic .NET dictionary, call the NET.createDictionary function.

.NET object – System.type

System.type

You can create a .NET object using its class constructor or use a .NET object returned by a member of the class. When a class member returns a .NET object, MATLAB leaves it as a .NET object.

  • variable of the underlying <ValueType>

  • null value, []

  • System.Nullable<ValueType> object

System.Nullable<ValueType>, with <ValueType> in C#

-

Pass MATLAB Cell Arrays to .NET

You can pass a cell array to a .NET property or function that expects an array of System.Object or System.String elements, as shown in this table.

MATLAB Type

Possible .NET Types
(listed from closest match to least close match)

Cell array
(Cannot exclusively contain string scalars or character arrays.)

  • System.Object[]

  • System.Object

Cell array of string scalars, character arrays, or both

  • System.String[]

  • System.Object[]

  • System.Object

The elements of a cell array can be any type supported for conversion, including other cell arrays.

Pass MATLAB Arrays to .NET

For information about passing MATLAB arrays to .NET methods, see Use MATLAB Arrays with .NET Functions.

How Array Dimensions Affect Conversion

The dimension of a .NET array is the number of subscripts required to access an element of the array. To get the number of dimensions, use the Rank property of the .NET System.Array type. The dimensionality of a MATLAB array is the number of nonsingleton dimensions in the array.

When converting arguments, MATLAB matches the array dimensionality with the .NET method signature, as long as the dimensionality of the MATLAB array is lower than or equal to the expected dimensionality. For example, you can pass a scalar input to a method that expects a 2-D array.

For a MATLAB array with number of dimensions, N, if the .NET array has fewer than N dimensions, the MATLAB conversion drops singleton dimensions, starting with the first one, until the number of remaining dimensions matches the number of dimensions in the .NET array.

Converting a MATLAB Array to System.Object

You can pass a MATLAB array to a function that expects a System.Object.

Pass MATLAB Arrays as Jagged Arrays

A MATLAB array is a rectangular array. .NET supports a jagged array, which is an array of arrays. The elements of a jagged array can be of different dimensions and sizes.

Although .NET languages support jagged arrays, the term jagged is not a language keyword. C# function signatures use multiple pairs of square brackets ([][]) to represent a jagged array. In addition, a jagged array can be

  • Nested (for example, [][][])

  • Multidimensional (for example, [,])

  • Nested with multidimensional elements (for example, [,,][,][])

MATLAB automatically converts MATLAB arrays of numeric types to the corresponding jagged array type. If the input argument is a nonnumeric type or multidimensional, use the NET.createArray function to create an array to pass as a jagged array. For examples, see Pass Jagged Arrays.

Pass Nonprimitive .NET Objects

To call a method with a .NET class argument, pass an object that is an instance of that class or its derived classes. You can create such an object using the class constructor, or use an object returned by a member of the class. When a class member returns a .NET object, MATLAB leaves it as a .NET object. Use this object to interact with other class members.

Unsupported MATLAB Types

MATLAB does not support passing these MATLAB types to .NET methods:

  • Structure arrays

  • Sparse arrays

  • Complex numbers

  • datetime

  • duration

  • table

See Also

Topics