Using deploytool to create Java package
古いコメントを表示
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
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.
採用された回答
その他の回答 (1 件)
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.
カテゴリ
ヘルプ センター および File Exchange で Classification Ensembles についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!