How to use matlab with WEKA
Matlab is a great environment for prototyping algorithms, WEKA is a great
environment for setting up and running experiments. Here is how to use
them together. (Of course, you need to already have Matlab, WEKA, and Java installed!)
- First, you or your system administrator will need to download and install Stefan Muller's JMatLink. This very nice piece of work allows one to use the matlab engine directly
from java. The JMatLink site contains makefiles for Windows and Solaris. Here is the makefile I used to build JMatLink on linux. Once you've built JMatLink, try it out by running the included TestGui app (e.g. "java TestGui"). When you start TestGui, you should see the matlab splash screen pop up. Once in TestGui, the first thing you need to do is open your connection to matlab by clicking on the "Open" button. When you open a matlab instance, you should see the matlab spash screen pop up. Once the matlab instance has started up, you can try typing commands into the text area and then hitting the "Eval(String)" button. A good one to try is "peaks" which will pop up a graph of a surface. Once you've verified that TestGui is working, you can try integrating matlab into WEKA.
- If WEKA is not already installed on your system, you or your system administrator will need to download and install it from here. Before you can start adding your matlab stuff to WEKA, you'll need to understand the WEKA class hierarchy and design patterns (detailed in the documentation). Once you've done that, here is the basic procedure for incorporating a matlab script into WEKA:
- Make sure that you import "JMatLink" in your file.
import JMatLink;
- You'll also need, within the body of your routine, to create a JMatLink communication object:
private JMatLink engine;
- At the point in your program where you want to use matlab, you'll need to instantiate the JMatLink object:
engine = new JMatLink();
and then open it:
engine.engOpen();
To actually use matlab on data that you are using in WEKA, you'll need to send an array to the matlab engine:
engine.engPutArray("array",array);
You can then operate on the array, just like you were in matlab:
engine.engEvalString("training_set=array(:,1:4)'");
engine.engEvalString("result = kmeans(training_set,"+m_NumClusters+")"); (Note that m_NumClusters is a variable in the WEKA code.)
And then you can retrieve your result from matlab:
double[][] result = engine.engGetArray("result");
And close the connection to matlab (Remember that each instance uses a matlab license!):
engine.engClose();
That's it! If you have a specific problem (and you're a LANS member), please feel free to email me and I'll help you out.