It's difficult to construct a Java array of primitives in Matlab, because Matlab wants to autobox it back into a Matlab array.
What you can do is create a Java class to help you, using the method signatures to guide Matlab's autoboxing. A wrapper layer like this may be faster and more convenient than a round trip through a text export.
package test;
/**
* Class to help build Java arrays from Matlab.
*/
public class JavaArrayBuilder {
/**
* Assign an array into a larger ragged array
* @param array ragged array you're building
* @param i index into array
* @param subarray this gets autoboxed to int[] from Matlab
*/
public static void assignIntArray(Object[] array, int i, int[] subarray) {
array[i] = subarray;
}
}
Then you can call it from Matlab like this.
function ja = build_int_array
mynums = { 1:2, 1:5, 1:7 };
% Create a Java array of arrays
dummy = java.lang.Object();
ja = java.lang.reflect.Array.newInstance(dummy.getClass(), numel(mynums));
for i = 1:numel(mynums)
test.JavaArrayBuilder.assignIntArray(ja, i-1, mynums{i});
end
% Now you have a Java ragged array, albeit as Object[] instead of int[][]
Afterwards, you'll need to convert the Object[] array to int[][] within Java, because Matlab will unbox a Java int[][] back to a Matlab array. Keeping it as Object[] within M-code protects it.
You could also build a List or other Collection using similar wrappers. That might mesh better with your other Java code, and Collections don't unbox in Matlab.