title: How to retrieve class values from WEKA using MATLAB
body: <p>I'm trying to retrieve classes from WEKA using MATLAB and WEKA API. All looks fine but classes are always 0. Any idea ??</p>
  
  <p>My data set has 241 atributes, applying WEKA to this dataset I'm obtaining correct results.</p>
  
  <p>1st train and test objects are created than classifier is build and classifyInstance performed. But this give wrong result</p>
  
  <pre><code>    train = [xtrain ytrain];
      test =  [xtest];
  
      save ('train.txt','train','-ASCII');    
      save ('test.txt','test','-ASCII');
  
  %## paths
  WEKA_HOME = 'C:\Program Files\Weka-3-7';
  javaaddpath([WEKA_HOME '\weka.jar']);
  
  fName = 'train.txt';
  
  %## read file
  
  loader = weka.core.converters.MatlabLoader();
  
  loader.setFile( java.io.File(fName) );
  train = loader.getDataSet();
  train.setClassIndex( train.numAttributes()-1 );
  
  % setting class as nominal
  
  v(1) = java.lang.String('-R');
  v(2) = java.lang.String('242');
  options = cat(1,v(1:end));
  
  filter = weka.filters.unsupervised.attribute.NumericToNominal();
  filter.setOptions(options); 
  filter.setInputFormat(train);   
  train = filter.useFilter(train, filter);
  
  fName = 'test.txt';
  
  %## read file
  
  loader = weka.core.converters.MatlabLoader();
  
  loader.setFile( java.io.File(fName) );
  test = loader.getDataSet();
  
  %## dataset
  relationName = char(test.relationName);
  numAttr = test.numAttributes;
  numInst = test.numInstances;
  
  %## classification
  classifier = weka.classifiers.trees.J48();
  classifier.buildClassifier( train );
  fprintf('Classifier: %s %s\n%s', ...
      char(classifier.getClass().getName()), ...
      char(weka.core.Utils.joinOptions(classifier.getOptions())), ...
      char(classifier.toString()) )
  
  classes =[];
  
  for i=1:numInst
  
       classes(i) = classifier.classifyInstance(test.instance(i-1));
  
  
  end
  </code></pre>
  
  <hr>
  
  <p>Here is a new code but still not working - classes = 0. Output from Weka for the same algo and data set is OK</p>
  
  <p>=== Detailed Accuracy By Class ===</p>
  
  <pre>
                 TP Rate   FP Rate   Precision   Recall  F-Measure   ROC Area  Class
                   0.99      0.015      0.985     0.99      0.988      0.991    0
                   0.985     0.01       0.99      0.985     0.988      0.991    1
  Weighted Avg.    0.988     0.012      0.988     0.988     0.988      0.991
  </pre>
  
  <p>=== Confusion Matrix ===</p>
  
  <pre>
      a    b   &lt;-- classified as
   1012   10 |    a = 0
  
     15 1003 |    b = 1
  </pre>
  
  <p>&nbsp;    </p>
  
  <pre><code>    ytest1 = ones(size(xtest,1),1); 
  
      train = [xtrain ytrain];
      test =  [xtest ytest1];
  
      save ('train.txt','train','-ASCII');    
      save ('test.txt','test','-ASCII');
  
  %## paths
  WEKA_HOME = 'C:\Program Files\Weka-3-7';
  javaaddpath([WEKA_HOME '\weka.jar']);
  
  fName = 'train.txt';
  
  %## read file
  
  loader = weka.core.converters.MatlabLoader();
  
  loader.setFile( java.io.File(fName) );
  train = loader.getDataSet();
  train.setClassIndex( train.numAttributes()-1 );
  
  v(1) = java.lang.String('-R');
  v(2) = java.lang.String('242');
  options = cat(1,v(1:end));
  
  filter = weka.filters.unsupervised.attribute.NumericToNominal();
  filter.setOptions(options); 
  filter.setInputFormat(train);   
  train = filter.useFilter(train, filter);
  
  fName = 'test.txt';
  
  %## read file
  
  loader = weka.core.converters.MatlabLoader();
  
  loader.setFile( java.io.File(fName) );
  test = loader.getDataSet();
  
  filter = weka.filters.unsupervised.attribute.NumericToNominal();
  filter.setOptions( weka.core.Utils.splitOptions('-R last') );
  filter.setInputFormat(test);   
  test = filter.useFilter(test, filter);
  
  
  %## dataset
  relationName = char(test.relationName);
  numAttr = test.numAttributes;
  numInst = test.numInstances;
  
  %## classification
  classifier = weka.classifiers.trees.J48();
  
  classifier.buildClassifier( train );
  fprintf('Classifier: %s %s\n%s', ...
      char(classifier.getClass().getName()), ...
      char(weka.core.Utils.joinOptions(classifier.getOptions())), ...
      char(classifier.toString()) )
  
  classes = zeros(numInst,1);
  for i=1:numInst   
       classes(i) = classifier.classifyInstance(test.instance(i-1));     
  end
  </code></pre>
  
  <p>here is a code snippet for class distribution in Java</p>
  
  <pre><code>// output predictions
      System.out.println("# - actual - predicted - error - distribution");
      for (int i = 0; i &lt; test.numInstances(); i++) {
        double pred = cls.classifyInstance(test.instance(i));
        double[] dist = cls.distributionForInstance(test.instance(i));
        System.out.print((i+1));
        System.out.print(" - ");
        System.out.print(test.instance(i).toString(test.classIndex()));
        System.out.print(" - ");
        System.out.print(test.classAttribute().value((int) pred));
        System.out.print(" - ");
        if (pred != test.instance(i).classValue())
      System.out.print("yes");
        else
      System.out.print("no");
        System.out.print(" - ");
        System.out.print(Utils.arrayToString(dist));
        System.out.println();
  </code></pre>
  
  <p>I converted it to MATLAB code like this</p>
  
  <pre><code>classes = zeros(numInst,1);
  for i=1:numInst
       pred = classifier.classifyInstance(test.instance(i-1));  
       classes(i) = str2num(char(test.classAttribute().value(( pred))));
  end
  </code></pre>
  
  <p>but classes are output incorrectly. </p>
  
  <p>In your answer you dont show that pred contains classes and predProb probabilities. Just print it !!!</p>