MATLAB: How to return multiple parameters from Matlab to a Java program

matlab builder ja multiple output parameters java test

I am making a Java program that must call some matlab functions. I have received some simple test programs to play with so I can learn how it all works.
My problem is that one of the matlab test programs returns two parameters, but in my Java program I only see/get one.
Here is the matlab code:
function [out1,out2]=test_Java5(in1,in2)
%


% [out1,out2]=test_Java5(in1,in2)
%
% out1='ABC';
% out2='DEF';
%
disp(in1);
disp('');
disp(in2);
out1='ABC';
out2='DEF';
end
Here is the Java method that calls matlab and prints output:
private static void testJava5(Testfunctions test) {
MWCharArray a = null;
MWCharArray b = null;
Object[] result = null;
try {
System.out.println("Starting testJava5");
a = new MWCharArray("Mary Smith");
b = new MWCharArray("John Doe");
result = test.test_Java5(1, a, b);
System.out.println("Results=" + result.length);
for (int i=0; i < result.length; i++) {
System.out.println("Type=" + result[i].getClass() + ", Result=" + result[i]);
}
System.out.println("Finished testJava5");
} catch (Throwable t) {
t.printStackTrace();
} finally {
a.dispose();
b.dispose();
MWArray.disposeArray(result);
}
}
Here is the output from my Java program:
Starting testJava5
Mary Smith
John Doe
Results=1
Type=class com.mathworks.toolbox.javabuilder.MWCharArray, Result=ABC
Finished testJava5
As you can see from the output, matlab prints out the two input parameters given to it, but the Java program only receive one parameter back as a MWCharArray (string).
I would have expected that my result array contained two records, or that result[0] was of type MWArray which contained the two MWCharArrays. But apperently not.
What am I doing wrong here?
PS: I know that I can use a struct instead, but we have a very complex set of input and outputs that would make it easier if it was separated.

Best Answer

AFAIK, the first argument that you pass into the method is the expected number of output arguments. So all you need to do is change:
result = test.test_Java5(1, a, b);
to:
result = test.test_Java5(2, a, b);