Documentation

Rules for Data Conversion Between .NET and MATLAB

Managed Types to MATLAB Arrays

The following table lists the data conversion rules used when converting native .NET types to MATLAB® arrays.

Note

The conversion rules listed in these tables apply to scalars, vectors, matrices, and multidimensional arrays of the native types listed.

Conversion Rules: Managed Types to MATLAB Arrays

Native .NET TypeMATLAB ArrayComments

System.Double

double

System.Single

single

Available only when the makeDouble constructor argument is set to false. The default is true, which creates a MATLAB double type.

System.Int64

int64

System.Int32

int32

System.Int16

int16

System.Byte

int8

System.String

char

None

System.Boolean

logical

None

MATLAB Arrays to Managed Types

The following table lists the data conversion rules used when converting MATLAB arrays to native .NET types.

Note

The conversion rules apply to scalars, vectors, matrices, and multidimensional arrays of the listed MATLAB types.

Conversion Rules: MATLAB Arrays to Managed Types

MATLAB Type

.NET Type (Primitive)

.NET Type (Class)

Comments

cell

N/A

MWCellArray

Cell and struct arrays have no corresponding .NET type.

structure

N/A

MWStructArray

char

System.String

MWCharArray

double

System.Double

MWNumericArray

Default is type double.

single

System.Single

MWNumericArray

uint64

System.Int64

MWNumericArray

Conversion to the equivalent unsigned type is not supported

uint32

System.Int32

MWNumericArray

Conversion to the equivalent unsigned type is not supported

uint16

System.Int16

MWNumericArray

Conversion to the equivalent unsigned type is not supported

uint8

System.Byte

MWNumericArray

None

logical

System.Boolean

MWLogicalArray

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

NumericType

  • System.Double

  • System.Single

  • System.Byte

  • System.Int16

  • System.Int32

  • System.Int64

  • System.Int64

numeric

System.Boolean

logical

System.Char

char

System.String

NumericType[N]

NumericType[1,N]

NumericType[Pn,..,P1,M,N]

NumericType[M,N,P1,..,Pn]

System.Boolean[N]

logical [1,N]

System.Boolean[Pn,..,P1,M,N]

logical [M,N,P1,..,Pn]

System.Char[N]

char [1,N]

System.Char[Pn,..,P1,M,N]

char [M,N,P1,..,Pn]

System.String[N]

char [N,max_string_length]

System.String[Pn,..,P1,N]

char [N,max_string_length, P1,..,Pn]

Scalar .NET structMATLAB struct constructed from public instance fields of the .NET struct
.NET struct [ N ]

MATLAB struct [1,N] where each element is constructed from public instance fields of the .NET struct

.NET struct [ M,N ]

MATLAB struct [ M,N ] where each element is constructed from public instance fields of the .NET struct

native.MWStructArray struct
native.MWCellArray cell
Hashtable struct
Dictionary <K,V>

Where K = string and V = scalar or array of [Numeric, boolean, Char, String]

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:
numericScalar

The type must be scalar in MATLAB. For example, a 1 X 1 int in MATLAB.

Vector

The type must be a vector in MATLAB. For example, a 1 X N or N X 1 int array in MATLAB.

N-dimensional array

The N-dimensional array type specified by the user must match the rank of the MATLAB numeric array.

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:
charCharThe char must be scalar.
Char array

The N-dimensional Char type must match the rank of the MATLAB char array.

String

MATLAB char array must be [1,N]

String array

The N-dimensional MATLAB char array can be converted to (N-1)-dimensional array of type String.

Conversion Rules: MATLAB Logical Arrays to .NET Types

To Convert This MATLAB Type:To this:Follow these rules:
logicalBooleanThe logical must be scalar.
Boolean[]

The MATLAB logical array must be [1,N] or [N,1].

Boolean array

The N-dimensional Boolean array must match the rank of the MATLAB logical array.

Conversion Rules: Cell Array to .NET Types

To Convert This MATLAB Type:To this:Follow these rules:
cellSystem.Array

The N-dimensional MATLAB cell array is converted to an N-dimensional System.Array of type object.

ArrayListThe 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 structThe 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 struct can be converted to a Hashtable. Any nested struct will also be converted to a Hashtable. If the nested struct is not a scalar, then an ArgumentException is thrown. The dictionary key must be of type String.

Conversion Rules: .NET Objects in MATLAB to .NET Native Objects

To Convert this MATLAB Type:To this:Follow these rules:
.NET objectType or super-type of the containing objectA .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.