View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Matlab Weka Interface

4.1 | 18 ratings Rate this file 114 Downloads (last 30 days) File Size: 76.2 KB File ID: #21204 Version: 1.0

Matlab Weka Interface


Matt Dunham (view profile)


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, Using Weka In Matlab, and Weka Lab: Bridging Weka And Matlab.

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 (44)
16 Jan 2017 Bojana Andjelkovic

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

Comment only
24 Sep 2016 WENJIE HAN

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?

Comment only
27 Jul 2016 Behrang

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:

Comment only
20 Jun 2016 Joly.Z

Joly.Z (view profile)

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

Comment only
16 Jun 2016 Sunny Lightman

16 Jun 2016 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.

07 Jun 2016 Joly.Z

Joly.Z (view profile)


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;

Comment only
08 Mar 2016 Ryan Hsu

31 Jan 2016 p sl

p sl (view profile)

31 Aug 2015 Karim Hardani

06 Aug 2015 ali asghari

20 Mar 2015 Sunghoon Lee

Sunghoon Lee (view profile)

  • 1 file
  • 4.71429

Sorry for multiple posting. I also uploaded the code at

Comment only
20 Mar 2015 Sunghoon Lee

Sunghoon Lee (view profile)

  • 1 file
  • 4.71429

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,

14 Mar 2015 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.

Comment only
11 Mar 2015 Ricardo

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,

Comment only
21 Dec 2014 Asdrubal Lopez Chau

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

Comment only
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;

Comment only
04 Oct 2014 mike

mike (view profile)

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?

Comment only
08 Sep 2014 chen

chen (view profile)

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

Rossana (view profile)

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!

Comment only
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 ?

Comment only
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

Comment only
30 Oct 2013 Feiyu Xiong

24 Jul 2013 parmin

parmin (view profile)

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?

Comment only
23 Jul 2013 parmin

parmin (view profile)

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

Comment only
30 Jun 2013 ARAM

ARAM (view profile)

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

Comment only
24 Jun 2013 Marc

Marc (view profile)

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?

Comment only
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

Comment only
20 Nov 2012 reza

reza (view profile)

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

21 Apr 2012 Nicole

Nicole (view profile)

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.

Comment only
28 Feb 2012 W W

W W (view profile)

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?

Comment only
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.

Comment only
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

Jorge (view profile)

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?

Comment only
13 May 2010 Matteo

Matteo (view profile)

Very useful!

21 Apr 2009 Philip

Philip (view profile)

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!

Comment only
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?

Comment only
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:

Comment only

Contact us