File Exchange

image thumbnail

Matlab Weka Interface

version (76.2 KB) by Matt Dunham
Matlab interface for Weka Classifiers


Updated 25 Aug 2008

View License

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

Cite As

Matt Dunham (2021). Matlab Weka Interface (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (52)

In case you are experiencing issues of any kind with ".Instance". This is due to the version of WEKA being 3.8 in 2020 and that the MatLab script is compatible with WEKA 3.5. If you want 3.6 go for the other script provided by Mr. Lee S. ( Don't forget to use "javaaddpath('C:\Program Files\weka-3-5-6\weka.jar')" at the top of the example script "wekaNBexample.m" for making your life easier and not having to modify "classifier.txt" in the MatLab rootfolder. You're are wellcome.

gökhan satilmis

Jan Motl

jun zhao

Does anyone knows how to solve this problem:
No constructor 'matlab2weka.convert2weka' with matching signature found.


this toolbox is very useful! but how to use it for the continuous fitting, and get the correct correlation coefficient?


i try to load the training file and the test file using the same function so i got error message "dupplicate loadARFF " how can i fix it ?

shafaq nisar

I want to do two class classifictaion using naiveBayes in matlab but unable to find the appropriate interface. how to import data and train ?(like SVM in classification leaner app)


Error: It always shows below:
Arguments to IMPORT must either end with ".*" or else specify a fully qualified class name:
"weka.core.converters.ArffLoader" fails this test.

Solution: See example from 19 Dec 2014 Asdrubal Lopez Chau

I adopted it, and it worked:

%Load from disk
fileDataset = 'training_solo_36_speakers_25_mfcc.arff';
myPath = 'C:\Users\workshit\';
javaaddpath('C:\Program Files\Weka-3-8\weka.jar');
wekaOBJ = loadARFF([myPath fileDataset]);

Bojana Andjelkovic

How to perform wekaClassify for unlabeled instance?
Thank you in advance!


It always shows below:
Arguments to IMPORT must either end with ".*" or else specify a fully qualified class name:
"weka.core.converters.ArffLoader" fails this test.
Error in KNN (line 1)
what should I do?


I got error "No constructor 'weka.core.Instance' with matching signature found." with weka 3.8 and Matlab R2016a.
Changed "Instance" to "DenseInstance" in line 59 of matlab2weka.m and it worked.
more info:


When 'No constructor 'weka.core.Instance' with matching signature found.' occurs, use weka-3-6-14

Sunny Lightman

Sunny Lightman

Dear Dunham, I am using your tools and it is very helpful. Thank you very much.

One crucial issue is that I am working on regression task, whereas your $wekaClassify()$ appears only deal with classification.

I modify this function by return $classProbs$ as the estimation. Is it correct?

Thank you very much.



please i have my research work in matlab code version. but i want to convert the code to java or how can I use the same code in weka? I want to compare the performance of the tools. i need a help.

my email;

Ryan Hsu

p sl

Karim Hardani

ali asghari

Sunghoon Lee

Sorry for multiple posting. I also uploaded the code at

Sunghoon Lee

The code worked great!! except that matlab2weka conversion was way too slow.... So, I have created similar package based on this work. For those who are interested,

saber nankali

I have a question about running feature selection algorithms of weka in matlab
does Any body know how i can change the option of a feature selection algorithm of weka in matlab? for example i want to decrease the criteria of error to select more features.


Hey guys. First i'd like to thank Matt Dunham for the files updloaded, they are really useful. I'm performing a study focused on finding the most relevant features for a classification task and i want to use the weka GUI for it, as i need to experiment several different classifiers and features combinations. To do that i am extracting features with matlab, and using these functions to create an arff file that i can use on weka.

My problem is i can't provide the expected labels for my objects in the matlab2weka function.

wekaClass=matlab2weka('Lesion Detection',featureNames,features(:,1:5),features(:,6));

I thought the last parameter, targetIndex should be it. features(:,6) is a vector containing the target classes for each object (0 or 1 as it is a dichotomous classification problem). Is targetIndex supposed to represent something different, or it could be me doing something wrong? I'm stuck with this problem and would be really glad to hear any suggestions.

Thanks in advance,

Asdrubal Lopez Chau

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

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;


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?


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


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!

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 ?

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

Feiyu Xiong


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?


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


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


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?

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


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


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.


Very easy to use

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?


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?


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?

Arturo Moncada-Torres


sorry - substantiated should of course be instantiated.


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.


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?


Very useful!



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!

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?

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:

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!