Using deploytool to create Java package

2 次查看(过去 30 天)
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 个评论
Emin BAKIR
Emin BAKIR 2016-6-1
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.

请先登录,再进行评论。

采纳的回答

Titus Edelhofer
Titus Edelhofer 2011-6-26
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 个评论
Steven Lord
Steven Lord 2015-9-14
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 2016-5-30
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.

请先登录,再进行评论。

更多回答(1 个)

Joan Puig
Joan Puig 2011-6-26
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 个评论
Artik Crazy
Artik Crazy 2011-6-30
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Classification Ensembles 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by