Is it possible to invoke a method in a Java class that accepts variable number of input arguments in MATLAB 7.7(R2008b)?
9 views (last 30 days)
Show older comments
Here is the sample Java code that accepts variable number of arguments:
public class TestVarArgs {
// calculate average of numbers of type 'double'
public static double average( double... numbers )
{
double total = 0.0; // initialize total
for ( double d : numbers )
total += d;
return total / numbers.length;
}
// calculate average of numbers of type 'Double'
public static Double averageD( Double... numbers )
{
Double total = 0.0;
for ( Double d : numbers )
total += d;
return total / numbers.length;
}
}
When I try to invoke any of the above methods in MATLAB as follows:
a = TestVarArgs
a.average(7,6)
a.averageD(java.lang.Double(23), java.lang.Double(34))
I get the following errors respectively:
??? No method 'average' with matching signature found for
class 'VarargsTest'.
??? No method 'averageD' with matching signature found for class
'VarargsTest'.
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
The ability to directly use Java methods that accept variable number of input arguments using the Java 5 syntax is not available in MATLAB.
To work around this issue, we can provide 'arrays' of the input argument type as follows:
% invoke the method that accepts variable number of native 'double' type
a = TestVarArgs;
numbers = [2,3,4,5]
a.average(numbers)
numbers = [2,3];
a.average(numbers)
%invoke the method that accepts variable number of class 'Double' type:
numbersD= javaArray('java.lang.Double',3)
numbersD(1)= java.lang.Double(10);
numbersD(2)= java.lang.Double(20);
numbersD(3)= java.lang.Double(30);
a.averageD(numbersD);
0 Comments
More Answers (0)
See Also
Categories
Find more on Call Java from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!