Using deploytool to create Java package

1 view (last 30 days)
Hello!
My intention is to use a Random Forest Ensemble, trained previously in Matlab, in a Java application. My Matlab function, which I compiled, using deploytool is:
function [Probability]=PredictingTest (Predictor)
load (['D:/Test/Tree.mat'], 'Tree')
[~, prob]=predict(Tree , Predictor);
Probability=prob(:,1);
end
Where:
  • Predictor input is an integer between -10 to 10.
  • Probability output is a double between 0 to 1.
  • Tree is a .mat file with a CompactTreeBagger object stored in it.
Next I deploy the jar files to my Eclipse project, and try to run this Java code:
/* Necessary package imports */
import com.mathworks.toolbox.javabuilder.*;
import PredictTest.*;
public class predict_test {
static MWNumericArray rhs = null; /* Stores input value */
static PredictTest prediction;
static Object[] result = null; /* Stores the result */
public static void main(String[] args) {
try {
prediction = new PredictTest();
rhs=new MWNumericArray(5,MWClassID.DOUBLE);
result=prediction.PredictingTest(1, rhs);
}
catch (MWException e) {
e.printStackTrace();
}}}
Sadly what I get is this exception:
{Warning: Variable 'Tree' originally saved as a CompactTreeBagger cannot be instantiated as an object and will be read in as a uint32.}
> In PredictingTest at 3
{??? Undefined function or method 'predict' for input arguments of type 'uint32'.
So, as far as I understood, this means that Java can't use Matlab objects even by the methods, compiled from Matlab functions. It will be very nice if I could get some help on how can I overcome this. It is very important to me. Thank you all in advance!
P.S I'm surely not confined to a Matlab TreeBagger algorithm. If there is a Java package you are familiar with, that can do the work, it can be a nice solution too.
  1 Comment
Emin BAKIR
Emin BAKIR on 1 Jun 2016
I know the question is quite old, but recently I face with a similar example, just in case if it helps to someone else... If you create an empty object of the not found class just before loading it, then Matlab will be able to instantiate that class.
For the above code, the Matlab code should be something like the following
function [Probability]=PredictingTest (Predictor)
Tree =CompactTreeBagger.empty;
load (['D:/Test/Tree.mat'], 'Tree')
[~, prob]=predict(Tree , Predictor);
Probability=prob(:,1);
end
I did not test the code, but it should work.

Sign in to comment.

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 26 Jun 2011
Hi,
try to add the CompactTreeBagger class definition to your project. Then you should be able to handle objects of this class within your project ...
Titus
  7 Comments
Steven Lord
Steven Lord on 14 Sep 2015
There's no reference to TreeBagger in the code. That is why you need to explicitly include the files in your project. When MATLAB Compiler tries to determine what it needs to include, it can't look inside the MAT-file (what if that MAT-file name was to be specified at runtime?) to determine that the MAT-file contains a TreeBagger object and realize that it should include the object definition in case you are loading that variable to use it.
Emin BAKIR
Emin BAKIR on 30 May 2016
How do we add the class definition to our project. is it with import statement? I was able to solve a similar problem by creating an empty object of the class first, then loaded the .mat file. But I would like to learn how do we add class definition. Thank you.

Sign in to comment.

More Answers (1)

Joan Puig
Joan Puig on 26 Jun 2011
For a more robust deployment I would also suggest you change this line:
load (['D:/Test/Tree.mat'], 'Tree')
The reason is that people using this tool might not even have a D: drive. You could make the file name an input to your function.
  1 Comment
Artik Crazy
Artik Crazy on 30 Jun 2011
Yeah, Thank you :)
I used it only to simplify the example.
There are a number of such trees, stored as .mat files, all created using different training sets. So the real function gets also a set of arguments that are used to choose the right tree to load and an input folder path as well.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!