File Exchange

image thumbnail

Using Weka in Matlab

version 1.5 (5.43 MB) by Sunghoon Lee
An efficient interface to use Weka in MATLAB


Updated 22 Jul 2015

View License

Weka is an open-source platform providing various machine learning algorithms for data mining tasks. Although Weka provides fantastic graphical user interfaces (GUI), sometimes I wished I had more flexibility in programming Weka. For instance, I often needed to perform the analysis based on leave-one-out-subject cross-validation, but it was quite difficult to do this on Weka GUI. I do most of my analyses on MATLAB, so I was searching for an interface between MATLAB and Weka. Fortunately, Weka was implemented in Java, and MATLAB had a wrapper that allows communicating with Java.
Here I introduce an efficient MATLAB to Weka interface, which was implemented based on the initial work of Matt Dunham.
This work is still in-progress and I have only included codes that I mainly use for my work. If you would like to collaborate to improve the code or if you find any bugs, please don't hesitate to reach me at "silee {at} partners {dot} org".
Also, please visit

Cite As

Sunghoon Lee (2020). Using Weka in Matlab (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (29)

Aamir Javed

Hello Sunghoon Lee. Can you update this code for latest version of Weka?

Hello Sunghoon Lee. Firstly thanks for this work. I get this error while I am trying to run classifier_example.m:
Error using wekaClassification (line 70)
Java exception occurred:
java.lang.NoClassDefFoundError: weka/core/FastVector

I have searched all around the internet and I couldn't find anything. I have downloaded WEKA 3.6 and I have the latest java version. Do you know what is the problem?

Sunghoon Lee

Dear users, this code would not work for the new WEKA 3.8; the code works perfectly for WEKA 3.6. Let me see if we can update this any soon! Thanks.

Sunghoon Lee

Hi Ana, "trainModel" variable in the wekaClassificaiton.m is the trained model. However, I doubt you can export the model in "java" variable from Weka Explorer... That means that you have to code yourself to run the specific classifier or regressor that you would like to have.. (just like Sunny's problem..) I don't personally have a plan to incorporate all the functions to my code as of now.. (just simply don't have time to do so...)

Hello Sunghoon Lee. Thanks for this work is very helpful. I have the same doubt as Sunny. For example, if you want to upload a model that you trained in the Weka Explorer, is it possible to load it to wekaRegression.m file?


Sunghoon Lee

Hi Sunny. My recommendation would be that you reference the WEKA javadoc of the regression algorithm that you would lik eto use (
Then, set up similarly by looking at my wekaRegression.m file! It is very intuitive and straight forward! Hope this helps..!!

Dear Sunghoon, I am considering to transfer my $weka2matlab$ tool from Dunham to yours.

While I found that in your codes, we cannot choose the regression base models freely: only four regressors have been equipped with your codes. I beg your pardon if I miss the important characters.

If so, could you please give me some hints on how to add more regressors and how to set their parameters? thank you very much.

If possible, we can contact via email in private.

Hi Sunghoon, thank you very much for your good job. But what if I want to use other weka regression models such as regression tree please?

Luu Thanh

hello, I have a feture_vector, how to predict where is class it belong to ?
Thank you !

Hi Sunghoon Lee

How to get model of decision tree and the accuracy?


How to get model of decision tree and the accuracy?

For my case I had to change:
predicted{z,1} = ft_test_weka.instance(z-1).classAttribute.value(trainModel.classifyInstance(ft_test_weka.instance(z-1))).char();% Modified by GM
predicted{z,1} = ft_train_weka.instance(z-1).classAttribute.value(trainModel.classifyInstance(ft_test_weka.instance(z-1))).char();% Modified by GM
to make it work because the ordering is different between train and test set if you use random subset cross-validation.

how to use this code, please give me tutorial

i figure what i had to do thank you exceptional code !!!

Hi Sunghoon.
Thanks to your magnificent work!
it was very helpful for my work

Hi Sunghoon.
Thanks to your magnificent work!
Now I've got a problem. Firstly I've tried to use the work of 'Matlab Weka Interface'
by Matt Dunham, to convert my .mat file into Weka'format. It works on Weka 3-6, and the return value is what I want.But Unfortunately it doesn't work on the Weka 3-7 series. Then I see your comment blow and follow the guide in your webside to use the java function 'convert2weka'. I didn't quite get the point how you use this function to make convertion. Can you show me how to use it to make convertion just like the work by 'Matlab Weka Interface'?


Sunghoon Lee

Hi Archana. If you have a JAVA Null Pointer Exception, it is most likely that you are not following the instruction to arrange the inputs accordingly. There is no need to convert the .mat to .arff since it is done automatically as part of the code. But you need to carefully program in MATLAB to do so. I suggest that you take a look at the tutorial that I have provided at It contains a number of examples of using regression, classification, and clustering algorithms. Hope this helps.

Thankyou for the code..
am getting the error"java null pointer exception"pls help me to clear the error in the code...
i want one suggestion, whether we can convert the .mat file into .arff file?
is this possible...





Sunghoon Lee,

Thanks for the reply.
I sent you the code and dataset.

Best Regards

Sunghoon Lee

Hi Awais.

The java error you got is not regarding the "heap" size. It is a simple "ArrayIndexOutOfBoundsException" error, meaning that the input may be not properly set up. Although it is best if I see your code, you can always refer to for more detailed information. Please read it carefully and organize the inputs accordingly!

Best regards,

Sunghoon Ivan Lee


Hi Sunghoon,
Very nice work indeed.

I tried to used J48 Decision Tree classifier, but it gives me the following error.
Error using wekaClassification (line 147)
Java exception occurred:
java.lang.ArrayIndexOutOfBoundsException: 2

at weka.classifiers.trees.j48.Distribution.prob(

at weka.classifiers.trees.j48.ClassifierSplitModel.classProb(

at weka.classifiers.trees.j48.ClassifierTree.getProbs(

at weka.classifiers.trees.j48.ClassifierTree.getProbs(

at weka.classifiers.trees.j48.ClassifierTree.classifyInstance(

at weka.classifiers.trees.J48.classifyInstance(

Although, i have increased the java heap memory from the matlab preferences, but still it gives me the same error.


thanks for you nice work, I'm used below code to have some number and percentage:

actualClassMat = cell2mat(actualClass);
predictedClassMat = cell2mat(predictedClass);
presicatPecent = sum(actualClassMat(:,7)==predictedClassMat(:,7))/N*100;


Sunghoon Lee

Hi Igor

Thank you for kind comments & suggestions, which are very helpful in refining the code!
I'm having hectic days recently, but I expect to upload the updated version within a couple days!! Thanks again :)

Hi Sunghoon,
thanks for the submission!

I just like to suggest the following minor edits, which, as I believe, would allow tipical "first time user" to properly configure his environment a bit faster:

1. IMHO, it's better to replace all pwd() in calls like "javaaddpath()" with "fileparts(mfilename('fullpath'))". It's a bit longer, but in this case there's no assumption about actual pwd()

2. IMHO, tipical first-time user would rather unpack WEKA to the folder next to "matlab2weka" folder...

3. It looks like "crossvalind" function is the only one, that requires Bioinformatics toolbox, which might be not installed, which might cause additional difficulties for some users.
In my case, I did the following replacement:
%idxCV = crossvalind('Kfold', N, K);
idxCV = round(rand([1 N])*K)+1;

as far as I understand, this is fairly the same.



1. Paths to WEKA has been updated to comply with Mac users. Thanks to Giovanni Mascia.
2. The "crossvalind" function, which requires the Bioinformatics toolbox, is replaced with idxCV = ceil(rand([1 N])*K)+1;. Thanks to Igor Varfolomeev

Small duplicated lines of code have been removed. Minor changes!

The input files for example codes have been added since some older versions of MATLAB don't have them built in.
The classifier & cost-sensitive classifier now produces "nominal outputs" rather than "numerical outputs". Thanks to Giovanni Mascia!

There was a small bug in wekaRegression.m and regression_example.m, which is "now" fixed.

There was a small bug in wekaRegression.m and regression_example.m, which is not fixed.

Correction: Bioinformatics Toolbox is not required!

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

Inspired by: Matlab Weka Interface

Inspired: Truss displacement based on FEM