Main Content

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.

The following is the MATLAB function signature for MethodStringArr.

Return TypeNameArguments
System.String[][] RetValMethodStringArr(NetPackage.StringClass this,
System.String[][] arr)

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.

The following is the MATLAB function signature for MethodMultiDArr. The input is a multidimensional jagged array that contains single dimensional elements.

Return TypeNameArguments
System.Double[][,] RetValMethodMultiDArr (NetPackage.NumericClass this,
System.Double[][,] arr)

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

Now you can pass arr to the MethodMultiDArr method.