MATLAB: Using deploytool to create Java package

builder jaMATLAB CompilerMATLAB Compiler SDKStatistics and Machine Learning Toolboxtreebagger

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.

Best Answer

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
Related Question