MATLAB: How to pass a javaArray of Doubles to a method that takes double[] arguments from MATLAB

booleancharintintegerjava.lang.booleanjava.lang.characterjava.lang.integerlogical?MATLAB

I am trying to call a Java method that takes a double array argument:
class mySum {
public static double sum(double[] in) {
double accum = 0;
for (int i = 0; i < in.length; i++) {
accum += in[i];
}
return accum;
}
}
When I inspect the class using METHODS:
methods('mySum','-full')
MATLAB reports the following signature:
...
static double sum(double[])
...
I instantiate a java array of Doubles using the JAVAARRAY function in MATLAB and then call the method:
ja = javaArray('java.lang.Double',2)
ja(1) = java.lang.Double(11)
ja(2) = java.lang.Double(12)
b = mySum.sum(ja)
However, I receive the following error: ERROR: ??? No method 'sum' with matching signature found for class 'mySum'.

Best Answer

java.lang.Double is an object wrapper around the Java double builtin data type. Arrays of java.lang.Doubles cannot be used as arguments to methods that expect double[].
To call methods with these signatures, use a MATLAB array of doubles:
ma = [11 12];
b = mySum.sum(ma)
This returns the expected result:
b =
23
Note that the argument is converted automatically, as the array "ma" above, is passed by value.
For more information about the passing data to Java functions from MATLAB see the link below.
Passing Built-In Types :: Using Sun Java Classes in MATLAB (External Interfaces)
For more information about the differences between double and java.lang.Double, refer to a Java resource.