Code covered by the BSD License  

Highlights from
Matlab Weka Interface


3.9 | 10 ratings Rate this file 224 Downloads (last 30 days) File Size: 76.2 KB File ID: #21204

Matlab Weka Interface



24 Aug 2008 (Updated )

Matlab interface for Weka Classifiers

| Watch this File

File Information

Weka is a comprehensive open source Machine Learning toolkit, written in Java at the University of Waikato, New Zealand. These functions provide a basic Matlab interface to Weka allowing you to transfer data back and forth and access major Weka features, such as training Classifiers. They have been tested with Weka version 3.5


This file inspired Matlab To Csv.

MATLAB release MATLAB 7.6 (R2008a)
Other requirements Requires Java and Weka, available here:
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (28)
21 Dec 2014 Asdrubal Lopez Chau

There is a problem with the source code I sent (some days ago). I am working on it.

19 Dec 2014 Asdrubal Lopez Chau

Complete example:

%Load from disk
fileDataset = 'cm1.arff';
myPath = 'C:\Users\Asdrubal\Google Drive\Respaldo\DoctoradoALCPC\Doctorado ALC PC\AlcMobile\AvTh\MyPapers\Papers2014\UnderOverSampling\data\Skewed\datasetsKeel\';
javaaddpath('C:\Users\Asdrubal\Google Drive\Respaldo\DoctoradoALCPC\Doctorado ALC PC\AlcMobile\JarsForExperiments\weka.jar');
wekaOBJ = loadARFF([myPath fileDataset]);
%Transform from data into Matlab
[data, featureNames, targetNDX, stringVals, relationName] = ...
%Create testing and training sets in matlab format (this can be improved)
[tam, dim] = size(data);
idx = randperm(tam);
testIdx = idx(1 : tam*0.3);
trainIdx = idx(tam*0.3 + 1:end);
trainSet = data(trainIdx,:);
testSet = data(testIdx,:);
%Trasnform the training and the testing sets into the Weka format
testingWeka = matlab2weka('testing', featureNames, testSet);
trainingWeka = matlab2weka('training', featureNames, trainSet);
%Now evaluate classifier
import weka.classifiers.*;
import java.util.*
wekaClassifier = javaObject('weka.classifiers.trees.J48');
e = javaObject('weka.classifiers.Evaluation',trainingWeka);
myrand = Random(1);
plainText = javaObject('weka.classifiers.evaluation.output.prediction.PlainText');
buffer = javaObject('java.lang.StringBuffer');
bool = javaObject('java.lang.Boolean',true);
range = javaObject('weka.core.Range','1');
array = javaArray('java.lang.Object',3);
array(1) = plainText;
array(2) = range;
array(3) = bool;

04 Oct 2014 mike

I am new to matlab.
I ve just downloaded weka toolkit because i want to load data from arff file.
How do i use loadARFF function in my script? can i find more info and examples somewhere?

08 Sep 2014 chen

Hi, Jing Wang, I have met the same problem with you. Have you solved the problem? Please help me, thank you!

11 Jun 2014 Rossana

Thanks for your helpful routine. But I have some trouble with string made up by just one character (for example gender: M/F). I obtain the following error message:

??? Java exception occurred:
java.lang.ClassCastException: java.lang.Character cannot
be cast to java.lang.String
at weka.core.Attribute.<init>(
at weka.core.Attribute.<init>(

Error in ==> matlab2weka at 37

Someone knows why??

If I add just one letter to the string, it works!

01 Apr 2014 Pierrick Azou

I would like to use the random forest classifier in matlab R2008 but I cannot find a way to pass the options to weka.train.classifier. I have tried to pass it like this : {'-I 10' '-K 10'} , {'-I 10','-K 10'} , '-I 10 -K 10' , ...;
but nothing seems to work. can You tell me what I'm doing wrong ?

30 Oct 2013 Feiyu Xiong

parmin, I have the same issue, I put class label as "?", then this will come out. I think it considers single character as Character in java, not String, that's why this happened. I don't know how to solve this yet

30 Oct 2013 Feiyu Xiong  
24 Jul 2013 parmin

I understand why this error appear.

??? Java exception occurred:
java.lang.ClassCastException: java.lang.Character cannot be cast to

at weka.core.Attribute.<init>(
at weka.core.Attribute.<init>(

Error in ==> matlab2weka at 37

when I want to send a single character such as "f" for values this errors caused. but for more than 1 character such as "no" there is no problem!
is there anybody to know how to do for single characters?

23 Jul 2013 parmin

Hi, I have problem like Marc! is there anyone to know how correct this errors?

30 Jun 2013 ARAM

I modified weka and added a new classification function to its functions.
but I need this function to my works in matlab but i can not read this function in matlab environment and i have a problem in classpath for this modified weka in matlab...please help me...thanks in advance ...ARAM

24 Jun 2013 Marc

Question about saving strings.

I have no problem saving an nxd array of numbers. However, when I then create an nxd cell with one column being filled with 'fail' and 'pass' I get the following error when executing matlab2weka:

??? Java exception occurred:
java.lang.ClassCastException: java.lang.Character cannot be cast to
at weka.core.Attribute.<init>(
at weka.core.Attribute.<init>(

Error in ==> matlab2weka at 37

How should I be inputing my data into matlab2weka for it to work?

16 Mar 2013 Jing Wang

I am new to weka. After "edit classpath.txt" and add "C:\Program Files\Weka-3-7\weka.jar" to that file, restart matlab, excute wekaNBexample, the following error occurs:

>> wekaNBexample
No constructor 'weka.core.Instance' with matching signature found.

Error in matlab2weka (line 50)
inst = Instance(numel(featureNames));

Error in wekaNBexample (line 21)
train = matlab2weka('iris-train',featureNames,train,classindex);

Could you help me?
Matlab R2012b
Weka 3.7.9

20 Nov 2012 reza

Passing options is easy. Try code below (in file trainWekaClassifier)
options = java.lang.String(options).split(' ');

21 Apr 2012 Nicole

I would like to report a bug:

I'm doing some cross-validation and my loop structure seems to work fine for Weka's Logistic classifier. However, when I try to do the exact same thing for AdaBoostM1, it throws the following error:

??? Java exception occurred:

Error in ==> wekaClassify at 24
classProbs(t+1,:) =

Error in ==> classifier_search at 225
[pred ~] = wekaClassify(matlab2weka('instance', featurelabels, tester),

I have determined through some testing that this only occurs when the number of instances in the training set is greater than the number of instances in the test set. I am sure you can see why that is a problem for me, since in most situations the training set is greater than the test set in size.

Is there something different about how I should format my inputs when using Adaboost rather than Logistic? Any information you can give regarding this problem would be so helpful. Hopefully it is just me and not your code.

28 Feb 2012 W W

Very easy to use

21 Jan 2012 shab shekan

Thank you so much.
I try it but I have problem.
I got this error " No method 'setValue' with matching signature found for class
Do you have any idea to fix this?

26 Oct 2011 Pierluigi Failla

I tried your code with using functions.SMO with parameter the standard Weka string:

'-C 1.0 -L 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1 -K "weka.classifiers.functions.supportVector.PolyKernel -C 250007 -E 1.0"'

and everything works fine, but when I try to do the same using lazy.IBk classifier with string parameter:

'-K 1 -W 0 -A "weka.core.neighboursearch.LinearNNSearch -A \"weka.core.EuclideanDistance -R first-last\""'

(copied from Weka) I get an Illegal Option error.

Do you have any idea to fix this?


26 Jul 2011 Kyle Heuton

Great code, but I can't figure out how to enter options that contain a numeric value, it has to be a cell array of strings, and I've tried:
{'-F','1'},{'-F 1'},{-F <1>}, and every other permutation I can think of, but none of them work.

What is the proper way to enter options that contain a value?

14 Jul 2011 Arturo Moncada-Torres  
28 Mar 2011 Kenneth

sorry - substantiated should of course be instantiated.

25 Mar 2011 Kenneth

Saved me a heap of time, thank you.

the matlab2weka file has a bit of a problem. "Instance" can't be substantiated. Quick work around is to use SparseInstance and create a filter in SaveAsArff to convert sparse to non-sparse.

26 Oct 2010 Jorge

Hi Matt,
I'm using weka-3.6 and matlab 7.6 (R2008a). The program loadARFF.m is experiencing the following error:
?? Java exception occurred: The specified source has Been
at weka.core.converters.ArffLoader.getDataSet ( 1003)
Error in ==> loadARFF at 20
wekaOBJ loader.getDataSet = ();

I am passing the file name correctly.
Can you please help?

13 May 2010 Matteo

Very useful!

21 Apr 2009 Philip  
07 Apr 2009 leptogenesis

As others have said, the usability could use some work. I just turned a .mat file into a .arff file using this, but at first, I was entering the data matrix the wrong way (should have used the transpose). When I did this, the program created the Instances object with no errors, and would then throw a cryptic nullpointerexception when I tried to write the arff file. It took half an hour going through the weka source code to figure out what was happening--a range check in your program would have saved me that time.

Otherwise, a very useful program!

23 Feb 2009 Sunny Mahajan

Hi Matt, I would find this extremely useful if I could use it.
I ran wekaNBexample.m got the following error:

??? Undefined function or variable 'FastVector'.

Error in ==> matlab2weka at 28
vec = FastVector();

Error in ==> wekaNBexample at 21
train = matlab2weka('iris-train',featureNames,train,classindex);

Can you please help?

31 Oct 2008 Fangqing Peng

what you have write here is so useful to me.
but can you list a intruduction about how to use your files here . thx.

my email:

Contact us