Using deploytool to create Java package

조회 수: 9 (최근 30일)
Artik Crazy
Artik Crazy 2011년 6월 26일
댓글: Emin BAKIR 2016년 6월 1일
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by