How to handle matrices in java

4 views (last 30 days)
Jani
Jani on 18 Jul 2011
[EDIT: 20110718 13:41 CDT - reformat - WDR]
I trying to call matlab function from java that takes two matrices as arguments.
I have to read the data file using java and after I have constructed the matrices (2 x 255 and 1 x 255) I call the matlab function compiled into java.
matlab:
data=load(L);
x = [data(:,1) data(:,2)];
y = data(:,3);
gp=gp_optim(gp,x,y); % gp is a structure
java:
data = main.readFile();
double[][] x = new double[255][2];
double[][] y = new double[255][1];
for (int i = 0; i < 255; i++) {
x[i][0] = data[i][0];
x[i][1] = data[i][1];
y[i][0] = data[i][2];
}
gpObj = li.gp_optim(1, gpObj[0], x, y);
Error:
com.mathworks.toolbox.javabuilder.MWException: Error using ==> chol
Matrix must be positive definite.
at com.mathworks.toolbox.javabuilder.internal.MWMCR.mclFeval(Native Method)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.access$600(MWMCR.java:23)
at com.mathworks.toolbox.javabuilder.internal.MWMCR$6.mclFeval(MWMCR.java:902)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.mathworks.toolbox.javabuilder.internal.MWMCR$5.invoke(MWMCR.java:800)
at $Proxy0.mclFeval(Unknown Source)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.invoke(MWMCR.java:475)
at gpStuff.GpStuff.gp_optim(GpStuff.java:557)
at main.Main.main(Main.java:132)
So it seems that I don't call the matlab function correctly. How the matrices should be handled in java?

Accepted Answer

Friedrich
Friedrich on 19 Jul 2011
Hi,
the BUILDER JA comes with a javabuilder.jar (located in C:\Program Files\MATLAB\R2011a\toolbox\javabuilder\jar) which provides MATLAB datatypes, MWARRAY ( <- abstract class). Please look here for the example how to handle matrices:
Click on the getfactor.java to see the java code. For the MWARRAY class definition see here:

More Answers (2)

Titus Edelhofer
Titus Edelhofer on 18 Jul 2011
Hi,
is this the MATLAB code?
data=load(L);
x = [data(:,1) data(:,2)];
y = data(:,3);
This will not work, since data will be a structure... But besides this: one easy way to find out what's going on: put into the first line of your MATLAB code some
if isdeployed
save c:\temp\inputdata.mat
end
Then compile and run, and in MATLAB you can do:
load inputdata
% call your function in debug mode ...
Titus
  2 Comments
Walter Roberson
Walter Roberson on 18 Jul 2011
data will not be a structure if what is being load()'d is a text file. As the documentation indicates,
S = load(filename) loads the variables from a MAT-file into a structure array, or data from an ASCII file into a double-precision array.
Titus Edelhofer
Titus Edelhofer on 18 Jul 2011
Hi Walter,
yes of course, it could be an ascii file, didn't think of this ...
Titus

Sign in to comment.


Jani
Jani on 19 Jul 2011
Thanks for your responses. Matlab loads the data as Walter mentioned, but the problem is with java. I don't know what kind of structure the matrix and vector should be. I have tested couple of different types build-in java, but nothing works. Could it be some matlab class (in javabuilder package) that corresponds to matlab matrix?

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!