How to return multiple parameters from Matlab to a Java program
Show older comments
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.
Accepted Answer
More Answers (3)
gb8
on 19 Sep 2012
0 votes
Another sample. Sum a[1,2] to b [1] and return y = a[1,2] + b[1]
Java Code
-------------***********************---------------
import java.util.ArrayList; import java.util.Arrays; import java.util.List;
import mTeste.mTESTEClass; import com.mathworks.toolbox.javabuilder.*;
public class teste {
/** * Launch the application. */ public static void main(String[] args) {
/** * Create the frame. */
mTESTEClass a = null; MWNumericArray b = null; MWNumericArray c = null; Object [] result = null; MWStructArray in = null; try {
a = new mTESTEClass(); double[] ex = {2,3}; b = new MWNumericArray(ex); c = new MWNumericArray(3); result = a.soma(1, b, c);
System.out.println(result.length); System.out.println(result[0]);
} catch (MWException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { b.dispose(); c.dispose(); MWArray.disposeArray(result); a.dispose(); } } }
-------------***********************---------------
MatLAB CODE -
Sum a[1,2] to b[1]
- FUNCTION SUM (matlab)
function y = soma( a, b )
%Summary of this function goes here % Detailed explanation goes here
y(1) = a(1) + b; y(2) = a(2) + b;
Neeraj
on 3 Nov 2012
0 votes
I wish to call an external java program on a different jvm from a matlab program while passing some parameters(preferably a java object). HOW IT IS POSSIBLE??
Mehmet Mahmudoglu
on 26 Dec 2016
Edited: Mehmet Mahmudoglu
on 28 Dec 2016
Basis Matlab Function matrixoperations just calculates the cross product of two vectors and adds the a constant value to a matrix.
function [vectorout,matrixout] = matrixoperations(vector_in1,vector_in2,matrix_in)
% vectorout output for cross product of two vectors
% matrixout is output for addition to matrix_in+ 4
% Example [vectorout,matrixout]
% =matrixoperations([1 3 4],[2 3 4],[3 3 4;1 2 3;3 4 5])
vectorout=cross(vector_in1,vector_in2);
matrixout=matrix_in+4;
multiple inputs and Outputs with Driver Java Class at the below
import com.mathworks.toolbox.javabuilder.*;
import matrixoperations.BasicMatrixOpClass;
public class BasicMatrixOperationsDriver {
public static void main(String[] args) {
/* Array of vector_in1 values */
MWNumericArray vector_in1 = null;
/* Array of vector_in2 values */
MWNumericArray vector_in2 = null;
/* Array of matrix_in values */
MWNumericArray matrix_in = null;
/* Stores BasicMatrixOp Class instance */
BasicMatrixOpClass runOperations= null;
Object[] result = null;
try {
double[] vector_in1_data= {1,3,4};
vector_in1 = new MWNumericArray
(vector_in1_data,MWClassID.DOUBLE);
double[] vector_in2_data= {2,3,4};
vector_in2 = new MWNumericArray
(vector_in2_data,MWClassID.DOUBLE);
double[][] matrix_in_data = {
{ 3, 3, 4 },
{ 1, 2, 3 },
{ 3, 4, 5 }
};
matrix_in =new MWNumericArray
(matrix_in_data,MWClassID.DOUBLE);
/* Create new BasicMatrixOpClass object */
runOperations=new BasicMatrixOpClass();
result=runOperations.matrixoperations
(2,vector_in1,vector_in2,matrix_in);
System.out.println();
System.out.println("vectorout");
System.out.println(result[0]);
System.out.println("matrixout");
System.out.println(result[1]);
} catch (MWException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
MWArray.disposeArray(vector_in1);
MWArray.disposeArray(vector_in2);
MWArray.disposeArray(matrix_in);
runOperations.dispose();
}
}
}
Categories
Find more on Java Package Integration 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!