Main Content

Handle MATLAB Data in .NET Applications

When you call a MATLAB® function that returns an argument, the engine attempts to convert the MATLAB data according to the type you specify for the variable. If the conversion fails, the engine throws System.InvalidCastException.

These tables show how the engine maps MATLAB data types to .NET data types. For information about size mapping, see MATLAB to .NET Array Conversion.

MATLAB Numeric Types in .NET

MATLAB Data Type

Supported .NET Data Type

double

System.Double
System.Double[]
System.Double[,,...]

single

System.Single
System.Single[]
System.Single[,,...]
System.Double

int8

System.SByte
System.SByte[]
System.SByte[,,...]
System.Int16
System.Int32
System.Int64
System.Single
System.Double
System.Decimal

uint8

System.Byte
System.Byte[]
System.Byte[,,...]
System.Int16
System.UInt16
System.Int32
System.UInt32
System.Int64
System.UInt64
System.Single
System.Double
System.Decimal

int16

System.Int16
System.Int16[]
System.Int16[,,...]
System.Int32
System.Int64
System.Single
System.Double
System.Decimal

uint16

System.UInt16
System.UInt16[]
System.UInt16[,,...]
System.Int32
System.UInt32
System.Int64
System.UInt64
System.Single
System.Double
System.Decimal

int32

System.Int32
System.Int32[]
System.Int32[,,...]
System.Int64
System.Single
System.Double
System.Decimal

uint32

System.UInt32
System.UInt32[]
System.UInt32[,,...]
System.Int64
System.UInt64
System.Single
System.Double
System.Decimal

int64

System.Int64
System.Int64[]
System.Int64[,,...]
System.Single
System.Double
System.Decimal

uint64

System.UInt64
System.UInt64[]
System.UInt64[,,...]
System.Single
System.Double
System.Decimal

complex double

Numerics.Complex
Numerics.Complex[]
Numerics.Complex[,,...]

logical

System.Boolean
System.Boolean[]
System.Boolean[,,...]

struct

MathWorks.MATLAB.Types.MATLABStruct
MathWorks.MATLAB.Types.MATLABStruct[]
MathWorks.MATLAB.Types.MATLABStruct[,,...]

all types in this table

System.Nullable<T> for type T in this table

Constants

Supported .NET Data Type

Result of NaN("double")

System.Double.NaN

Result of NaN("single")

System.Single.NaN

Result of Inf("double")

System.Double.PositiveInfinity

Result of Inf("single")

System.Single.PositiveInfinity

Result of -Inf("double")

System.Double.NegativeInfinity

Result of -Inf("single")

System.Single.NegativeInfinity

MATLAB String and Character Types in .NET

MATLAB Data Type

Supported .NET Data Type

string

System.String
System.String[]
System.String[,,...]

char

System.String
System.String[]
System.String[,,...]
System.Char
System.Char[]
System.Char[,,...]
System.Nullable<char>

<missing> value in string

null System.String

MATLAB Dictionary Type in .NET

The conversion is supported when:

  • The keys of the MATLAB dictionary are numeric, boolean, or non-missing string types.

  • When converting to a strongly-typed dictionary the TKey and TValue are the "closest match" according to Pass .NET Data Types to MATLAB Functions.

For examples, see Use MATLAB Dictionary Objects in .NET.

MATLAB Data Type

Supported .NET Data Type

dictionary

Any of these interfaces if TKey and TValue can be converted to native .NET types:

  • System.Collections.Generic.Dictionary<TKey,TValue>

  • System.Collections.Generic.IDictionary<TKey,TValue>

  • System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>

  • System.Collections.IDictionary

If TKey and TValue are specified, you must use these types to configure the MATLAB dictionary:

MATLAB Type

Required .NET Type

Required C# Keyword

Restrictions

logicalSystem.Booleanbool

None

int8System.SBytesbyte

None

uint8System.Bytebyte

None

int16System.Int16short

None

uint16System.UInt16ushort

None

int32System.Int32int

None

uint32System.UInt32uint

None

int64System.Int64long

None

uint64System.UInt64ulong

None

singleSystem.Singlefloat

None

doubleSystem.Doubledouble

None

stringSystem.Stringstring

<missing> keys not supported

value or handle class

MathWorks.MATLAB.Types.MATLABObject

None

Not supported as keys

struct

MathWorks.MATLAB.Types.MATLABStruct

None

cell

System.Objectobject

If the MATLAB dictionary is not configured (that is, dictionary.isConfigured == false), then the target .NET dictionary can use any type for keys and values. The resulting dictionary object is empty.

MATLAB to .NET Array 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 non-singleton dimensions in the array.

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.

If the MATLAB array is still of greater rank than the .NET array after removing leading dimensions, then MATLAB throws System.InvalidCastException.

Support for System.Array

All MATLAB types can be assigned to System.Array variables. The resulting array has the same dimensions as the MATLAB array with the equivalent underlying type.

MATLAB Objects and Cell Arrays in .NET

The .NET engine converts MATLAB handle and value objects and the elements of a cell array to the MathWorks.MATLAB.Types.MATLABObject or System.Array types. The MATLABObject type is an opaque type. You can pass these objects to MATLAB functions that know how to work with them, but your program does not have direct access to the properties and methods of the object.

To provide access to object data members, the engine supports the dynamic type in C# applications. For example, in this code you can access the UserData property and call the disp method on a MATLAB figure object.

dynamic fig = eng.figure();
fig.UserData = 3.14;
fig.disp( new RunOptions(nargout:0) );

This code shows how to access the elements of a MATLAB cell array. You must explicitly declare the appropriate type to get the native .NET type.

dynamic[ ] values = eng.eval(" { 3.14, 'Hello', uint8(42) } ");
double e1 = values[0];
string e2 = values[1];
byte e3 = values[2];

For the benefits of dynamic typing, refer to Microsoft® documentation, for example, Using type dynamic or Dynamic Language Runtime Overview. These features are available on all platforms that support C# 4, including .NET Framework and .NET Core.

Related Topics