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 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
complex |
|
|
|
| MathWorks.MATLAB.Types.MATLABStruct MathWorks.MATLAB.Types.MATLABStruct[] MathWorks.MATLAB.Types.MATLABStruct[,,...] |
all types in this table |
|
Constants | Supported .NET Data Type |
---|---|
Result of | System.Double.NaN |
Result of | System.Single.NaN |
Result of | System.Double.PositiveInfinity |
Result of | System.Single.PositiveInfinity |
Result of | System.Double.NegativeInfinity |
Result of | System.Single.NegativeInfinity |
MATLAB String and Character Types in .NET
MATLAB Data Type | Supported .NET Data Type |
---|---|
|
|
|
|
| null |
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
andTValue
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
|
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 |
---|---|---|---|
logical | System.Boolean | bool | None |
int8 | System.SByte | sbyte | None |
uint8 | System.Byte | byte | None |
int16 | System.Int16 | short | None |
uint16 | System.UInt16 | ushort | None |
int32 | System.Int32 | int | None |
uint32 | System.UInt32 | uint | None |
int64 | System.Int64 | long | None |
uint64 | System.UInt64 | ulong | None |
single | System.Single | float | None |
double | System.Double | double | None |
string | System.String | string |
|
value or handle class | MathWorks.MATLAB.Types.MATLABObject | None | Not supported as keys |
struct | MathWorks.MATLAB.Types.MATLABStruct | None | |
cell | System.Object | object |
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.