Pass MATLAB Data to .NET Functions
When you call a .NET method or function from MATLAB®, MATLAB converts arguments into .NET types. MATLAB performs this conversion on each passed argument, except for arguments that are already .NET objects.
For information about using .NET data in MATLAB instead, see Handle Data Returned from .NET Objects.
Pass MATLAB Scalar Data to .NET
This table shows MATLAB types for input data and the corresponding possible .NET types.
MATLAB chooses a .NET type depending on the argument type that the .NET function
expects. To view a .NET function signature including expected argument types, use the
methodsview function.
MATLAB Type | Possible .NET Types | Additional Information |
|---|---|---|
logical |
|
|
double |
|
|
single |
|
|
int8 |
|
|
uint8 |
|
|
int16 |
|
|
uint16 |
|
|
int32 |
|
|
uint32 |
|
|
int64 |
|
|
uint64 |
|
|
empty | System.Nullable | MATLAB converts empty |
char |
|
|
string |
| MATLAB automatically converts a |
| null |
|
string.empty |
|
|
dictionary |
|
|
.NET object –
| System. | You can create a .NET object using its class constructor or use a .NET object returned by a member of the class. When a class member returns a .NET object, MATLAB leaves it as a .NET object. |
| System.Nullable<ValueType>, with
<ValueType> in C# |
|
Pass MATLAB Cell Arrays to .NET
You can pass a cell array to a .NET property or function that expects an array of
System.Object or System.String elements, as
shown in this table.
MATLAB Type | Possible .NET Types |
|---|---|
Cell array |
|
Cell array of string scalars, character arrays, or both |
|
The elements of a cell array can be any type supported for conversion, including other cell arrays.
Pass MATLAB Arrays to .NET
For information about passing MATLAB arrays to .NET methods, see Use MATLAB Arrays with .NET Functions.
How Array Dimensions Affect 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 nonsingleton dimensions in the array.
When converting arguments, 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.
Converting a MATLAB Array to System.Object
You can pass a MATLAB array to a function that expects a
System.Object.
Pass MATLAB Arrays as Jagged Arrays
A MATLAB array is a rectangular array. .NET supports a jagged array, which is an array of arrays. The elements of a jagged array can be of different dimensions and sizes.
Although .NET languages support jagged arrays, the term jagged is
not a language keyword. C# function signatures use multiple pairs of square brackets
([][]) to represent a jagged array. In addition, a jagged array
can be
Nested (for example,
[][][])Multidimensional (for example,
[,])Nested with multidimensional elements (for example,
[,,][,][])
MATLAB automatically converts MATLAB arrays of numeric types to the corresponding jagged array type. If the
input argument is a nonnumeric type or multidimensional, use the NET.createArray function to create an array to pass as a jagged array.
For examples, see Pass Jagged Arrays.
Pass Nonprimitive .NET Objects
To call a method with a .NET class argument, pass an object that is an instance of that class or its derived classes. You can create such an object using the class constructor, or use an object returned by a member of the class. When a class member returns a .NET object, MATLAB leaves it as a .NET object. Use this object to interact with other class members.
Unsupported MATLAB Types
MATLAB does not support passing these MATLAB types to .NET methods:
Structure arrays
Sparse arrays
Complex numbers
datetimedurationtable