Calling MatLab from Java
From PlasmaWiki
Contents |
[edit] Overview
One of the consistent issues with prototyping algorithms in MatLab is trying to tie them into your software framework.
MatlabControl object from http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html allows a user to create calls into MatLab from Java. However, this object must be used and run from within the same JVM that MatLab is running.
There's a link from there to this site, http://www.cs.utoronto.ca/~bowen/code/code.html which sets up a MatlabServer. The MatlabServer allows remote calls, but everything must be string based, and it's using a basic socket server with no well defined protocol.
Building on this idea I decided to write my own based on XMLRPC. This would allow for a Client to remotely call into MatLab, have MatlabControl do any operation that's needed and still get back full Objects.
I built my server using the XMLRPC libraries from Apache. This is the most well used XMLRPC library for Java out there. The mini WebServer built into these libraries was a perfect server to use for this application since I didn't want the overhead of a full Tomcat implementation with full XMLRPC servlets.
The cool thing with using XMLRPC is that now this same server can be used from any XMLRPC client, including from C++ and .NET.
[edit] Download Library and Project
RemoteMatlab.zip - An Eclipse Java project containing the Server and Client code to remotely call MATLAB from Java using XMLRPC.
[edit] How-To
- Copy the exitform.fig, exitform.m, RemoteMatlab.jar and StartMatlabServer.m to your working directory.
- Copy the lib directory (and all it's contents) to your working directory.
- Run server
>> StartMatlabServer
[edit] File Descriptions
[edit] StartMatlabServer.m
A simple m file that sets up the java paths and starts the server. It will also show a form to allow for clean shutdown of the server and show a twiddler when running.
[edit] exitform.fig and exitform.m
A simple gui form that shows one button. Click the button to shutdown the server.
[edit] RemoteMatlab.jar
The compiled project files.
[edit] lib directory
xmlrpc libraries from apache and some commons libraries. The jmi.jar from MATLAB itself is there for compilation purposes.
[edit] Source Descriptions
[edit] MatlabControlImpl.java
A slight modification of the above named MatlabControl object from http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html.
[edit] MatlabControl.java
An interface that defines the functions in the MatlabControl implementation. This then is used on the client side as well.
[edit] MatlabServer.java
A slight modification of the code from Apache XMLRPC Server Page.
[edit] MatlabClient.java
A class that can be used from any Java application to call MATLAB remotely. Use the example in the main() method as a starting point.

