Clear Filters
Clear Filters

How to return multiple parameters from Matlab to a Java program

2 views (last 30 days)
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

Kaustubha Govind
Kaustubha Govind on 8 Jul 2011
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);
  2 Comments
Neeraj
Neeraj on 3 Nov 2012
I have tried the above code, but my program is unable to find TestFunctions class. I would be obliged if you respond regarding: (i) Which classes/packages to import in the above program. (ii) How to include TestFunctions class (iii) CAN YOU REFER TO ME SOME CODE USING WHICH I MAY BE ABLE TO CALL A MATLAB FUNCTION FROM AN EXTERNAL JAVA PROGRAM (outside of MATLAB) FROM OUTSIDE AND GET THE OUTPUT (may be an object) BACK TO MY JAVA PROGRAM (that is run from outside of MATLAB enviornment). (iv) I am using jdk1.7/jre7 but my matlab R2011b has jvm version 1.6. Can you tell me weather my matlab .jar files would be compatible with jre7/jdk1.7.

Sign in to comment.

More Answers (3)

gb8
gb8 on 19 Sep 2012
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
Neeraj on 3 Nov 2012
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
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!