Pass Jagged Arrays
Create System.Double .NET Jagged Array
This example shows how to create a .NET jagged array of
System.Double using the NET.createArray
function.
Create a three element array. You can pass jArr to any .NET method
with an input or output argument of type System.Double[][].
jArr = NET.createArray("System.Double[]",3)
jArr =
Double[][] with properties:
Length: 3
LongLength: 3
Rank: 1
SyncRoot: [1×1 System.Double[][]]
IsReadOnly: 0
IsFixedSize: 1
IsSynchronized: 0
Call .NET Method with System.String Jagged Array Arguments
This example shows how to create an array of MATLAB® character vectors to pass to a method,
MethodStringArr, with a System.String[][]
input argument.
This is the MATLAB function signature for MethodStringArr.
| Return Type | Name | Arguments |
|---|---|---|
System.String[][] RetVal | MethodStringArr | (NetPackage.StringClass
this, |
The MATLAB character vectors you want to pass to the method are:
str1 = {"this", "is"};
str2 = "jagged";
Create a variable, netArr, of
System.String arrays, which contains two arrays. Using
the NET.createArray, the typeName for
this array is System.String[], and the dimension is
2.
netArr = NET.createArray("System.String[]",2);
The arrays contain empty strings.
Create System.String arrays to correspond to the
MATLAB character vectors, str1 and
str2.
netArr(1) = NET.createArray("System.String",2); netArr(2) = NET.createArray("System.String",1);
Assign str1 and str2 to
netArr.
netArr(1) = str1; netArr(2,1) = str2;
Because str2 is a scalar and netArr(2)
expects an array, you must assign str2 to the specific
element netArr(2,1).
Now you can pass netArr to the
MethodStringArr method.
class(netArr)
ans = System.String[][]
Call .NET Method with Multidimensional Jagged Array Arguments
This example shows how to create a MATLAB array to pass to a method, MethodMultiDArr, with a
multidimensional jagged array input argument of System.Double
type.
This is the MATLAB function signature for MethodMultiDArr. The
input is a multidimensional jagged array that contains single dimensional
elements.
| Return Type | Name | Arguments |
|---|---|---|
System.Double[][,] RetVal | MethodMultiDArr | (NetPackage.NumericClass
this, |
Create a 2-by-3 array with
typeName of System.Double[].
arr = NET.createArray("System.Double[]",2,3);The elements are empty arrays.
The MATLAB arrays you want to pass to the method are:
A1 = [1 2 3]; A2 = [5 6 7 8];
MATLAB automatically converts a numeric array to the equivalent .NET type.
arr(1,1) = A1; arr(1,2) = A2;
Array arr is a System.Double[][,] jagged
array.
arr
arr =
Double[][,] with properties:
Length: 6
LongLength: 6
Rank: 2
SyncRoot: [1x1 System.Double[][,]]
IsReadOnly: 0
IsFixedSize: 1
IsSynchronized: 0Now you can pass arr to the
MethodMultiDArr method.
How MATLAB Handles Jagged Arrays
You must convert a .NET jagged array before using it in a MATLAB command. To convert:
If the shape of the array is rectangular, use the corresponding MATLAB numeric function.
If the array is not rectangular, use the
cellfunction.
If the jagged array is multidimensional, you must individually convert the arrays in each dimension.