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[,,...]

int8

System.SByte
System.SByte[]
System.SByte[,,...]

uint8

System.Byte
System.Byte[]
System.Byte[,,...]

int16

System.Int16
System.Int16[]
System.Int16[,,...]

uint16

System.UInt16
System.UInt16[]
System.UInt16[,,...]

int32

System.Int32
System.Int32[]
System.Int32[,,...]

uint32

System.UInt32
System.UInt32[]
System.UInt32[,,...]

int64

System.Int64
System.Int64[]
System.Int64[,,...]

uint64

System.UInt64
System.UInt64[]
System.UInt64[,,...]

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 and 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> (all dimensions)

<missing> value in string

null System.String

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