Rules for Data Conversion Between .NET and MATLAB
Note
The conversion rules listed in the following tables apply to scalars, vectors, matrices, and multidimensional arrays of the native types listed.
Managed .NET Types to MATLAB Arrays
The following table lists the data conversion rules used when converting native .NET types to MATLAB® arrays.
Conversion Rules: Managed Types to MATLAB Arrays
Native .NET Type | MATLAB Array | Comments |
---|---|---|
|
| — |
|
| Available only when the |
|
| |
|
| |
|
| |
|
| |
|
| None |
|
| None |
MATLAB Arrays to Managed .NET Types
The following table lists the data conversion rules used when converting MATLAB arrays to native .NET types.
Conversion Rules: MATLAB Arrays to Managed Types
MATLAB Type | .NET Type (Primitive) | .NET Type (Class) | Comments |
---|---|---|---|
| N/A |
| Cell and struct arrays have no corresponding .NET type. |
| N/A |
| |
|
|
| |
|
|
| Default is type double. |
|
|
| |
|
|
| Conversion to the equivalent unsigned type is not supported |
|
|
| Conversion to the equivalent unsigned type is not supported |
|
|
| Conversion to the equivalent unsigned type is not supported |
|
|
| None |
|
|
| None |
Function handle | N/A | N/A | None |
Object | N/A | N/A | None |
.NET Types to MATLAB Types
In order to create .NET interfaces that describe the type-safe API of a MATLAB Compiler SDK™ generated component, you must decide on the .NET types used for input and output parameters.
When choosing input types, consider how .NET inputs become MATLAB types. When choosing output types, consider the inverse conversion
The following tables list the data conversion results and rules used to convert .NET types to MATLAB arrays and MATLAB arrays to .NET types.
Note
Invalid conversions result in a thrown ArgumentException
Conversion Results: .NET Types to MATLAB Types
.NET Type | Converts to MATLAB Type |
---|---|
| numeric
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Scalar .NET struct | MATLAB struct constructed from |
.NET struct [ N ] | MATLAB struct |
.NET struct [ | MATLAB struct [ |
native.MWStructArray | struct
|
native.MWCellArray
| cell
|
Hashtable
| struct
|
| struct
|
ArrayList
| cell
|
Any other .NET type in the default application domain | .NET object |
Any other serializable .NET type in a non-default application domain | .NET object |
Conversion Rules: MATLAB Numeric Types to .NET Types
To Convert This MATLAB Type: | To this: | Follow these rules: |
---|---|---|
numeric | Scalar | The type must be scalar in MATLAB. For example, a 1 X 1 |
Vector | The type must be a vector in MATLAB. For example, a 1 X | |
-dimensional array | The |
Tip
When converting MATLAB
numeric
arrays, widening conversions are allowed. For example, an
int
can be converted to a double
. The type
specified must be a numeric type that is equal or wider. Narrowing conversions throw an
ArgumentException
.
Caution
.NET types are not as flexible as MATLAB types. Take care and test appropriately with .NET outputs before integrating data into your applications.
Conversion Rules: MATLAB Char Arrays to .NET Types
To Convert This MATLAB Type: | To this: | Follow these rules: |
---|---|---|
char | Char | The char must be scalar. |
Char array | The | |
String | MATLAB
| |
String array | The |
Conversion Rules: MATLAB Logical Arrays to .NET Types
To Convert This MATLAB Type: | To this: | Follow these rules: |
---|---|---|
logical | Boolean | The logical must be scalar. |
Boolean[] | The MATLAB
| |
Boolean array | The |
Conversion Rules: Cell Array to .NET Types
To Convert This MATLAB Type: | To this: | Follow these rules: |
---|---|---|
cell | System.Array | The |
ArrayList | The MATLAB
cell array must be a vector. |
Caution
If the MATLAB
cell
array contains a struct
, it is left
unchanged. All other types are converted to native types. Any nested
cell
array is converted to a System.Array
matching the dimension of the cell
array, as illustrated in this code
snippet:
Let C = {[1,2,3], {[1,2,3]},'Hello world'} % be a cell
C
can be converted to an
object[1,3]
where object[1,1]
contains
int[,]
, object[1,2]
contains an
object[1,1]
whose first element is an int[,]
,
and object[1,3]
contains char[,]
.Note
Any nested cell array is converted to a System.Array
that matches
the dimension of the cell
array
Conversion Rules: Struct to .NET Types
To Convert This MATLAB Type: | To this: | Follow these rules: |
---|---|---|
struct | .NET struct | The name and number of public fields in the specified .NET struct must match the name and number of fields in the MATLAB struct. |
Hashtable | A scalar |
Conversion Rules: .NET Objects in MATLAB to .NET Native Objects
To Convert this MATLAB Type: | To this: | Follow these rules: |
---|---|---|
.NET object | Type or super-type of the containing object | A .NET object in MATLAB can only be converted to a type or a super-type. |
Character and String Conversion
A native .NET string is converted to a
1
-by-N
MATLAB character array, with N
equal to the length of
the .NET string.
An array of .NET strings (string[]
) is converted to an
M
-by-N
character array, with
M
equal to the number of elements in the string
([]
) array and N
equal to the maximum
string length in the array.
Higher dimensional arrays of String
are similarly converted.
In general, an N
-dimensional array of
String
is converted to an
N
+1
dimensional MATLAB character array with appropriate zero padding where supplied strings have
different lengths.
Unsupported MATLAB Array Types
The MATLAB Compiler SDK product does not support returning the following MATLAB array types because they are not CLS-compliant:
int8
uint16
uint32
uint64
However, it is permissible to pass these types as arguments to a MATLAB Compiler SDK component.