Use MATLAB Arrays with .NET Functions
You can pass MATLAB® arrays to .NET functions and convert data returned from .NET functions to MATLAB arrays. For information on cell arrays, see Use Cell Arrays of .NET Data.
Pass MATLAB Arrays to .NET
When you call a .NET function from MATLAB, you can pass MATLAB array data as arguments.
If you pass a MATLAB numeric or string array, MATLAB automatically converts array elements into .NET types, as described in Pass MATLAB Scalar Data to .NET.
To pass an array of character arrays, create a cell array of character arrays to pass to the .NET function. For more information, see Pass MATLAB Cell Arrays to .NET.
For all other array types including jagged arrays, use the NET.createArray function in
MATLAB to create a .NET array to pass to the .NET function.
MATLAB creates a .NET array, copies the elements from the MATLAB array to the .NET array, and passes it to the function.
Convert Primitive .NET Arrays to MATLAB Arrays
When you call a .NET function from MATLAB, the function can return .NET data that you can convert to a MATLAB array. To convert a .NET array of a primitive .NET type to a MATLAB array, use a MATLAB conversion function.
For example, suppose that a .NET method returns netArr of type
System.Int32[].
netArr =
Int32[] with properties:
Length: 5
LongLength: 5
Rank: 1
SyncRoot: [1×1 System.Int32[]]
IsReadOnly: 0
IsFixedSize: 1
IsSynchronized: 0
Convert the array to a MATLAB array of type int32 by using the
int32 conversion function.
B = int32(netArr)
B = 1×5 int32 row vector 1 2 3 4 5
You can then use the converted array in MATLAB. For example, combine the elements in B with a
MATLAB array, C.
C = int32([11 12 13 14 15]); B + C
ans = 1×5 int32 row vector 12 14 16 18 20
For a list of MATLAB conversion functions, see Convert Arrays of Primitive .NET Type to MATLAB Type.
If the returned .NET data is in a .NET jagged array, use the
cell function. For more information, see Use Cell Arrays of .NET Data.
Access .NET Array Elements in MATLAB
You access elements of a .NET array with subscripts, just like with MATLAB arrays. For example, you can access the second element of a 1-by-5
.NET array A by using A(2).
For a multidimensional .NET array, you must specify the index for each dimension of the .NET array. You cannot refer to the elements of a multidimensional .NET array with a single subscript (linear indexing) like you can in MATLAB.
You can only use scalar indices to access elements of a .NET array. MATLAB does not support the colon operator for .NET arrays.
Use Get and Set Instance Functions
Alternatively, you can access elements of a .NET array using the
Set and Get instance functions. When
using Set or Get you must use C# array
indexing, which is zero-based.
For example, create two System.String arrays, one using the
Set function and the other using direct
assignment:
d1 = NET.createArray("System.String",3); d1.Set(0,"one"); d1.Set(1,"two"); d1.Set(2,"three"); d2 = NET.createArray("System.String",3); d2(1) = "one"; d2(2) = "two"; d2(3) = "three";
Compare the values of the first element in each array.
System.String.Compare(d1(1),d2.Get(0))
MATLAB displays 0, meaning the strings are
equal.