Invoke Matlab from Java

For a long time, this topic remained in the drafts of my mind, so finally decided to blog on it!

There are many reasons why would want to call matlab from Java.

Simple reason being, MATLAB is a technical computing language mainly used for algorithm development, data analysis, visualization, and numerical computation. Using MATLAB, you can solve technical computing problems faster than with programming languages, such as C, C++, and Java.

To differentiate your technical computing workspace, you probably need a traditional programming language such a C, Python or Java. I will be discussing here, how to invoke your Matlab program from Java.

Pre-requisites:

1. Matlab program (The program you wish to invoke. Optionally, you may also use an in-built Matlab function, in that case this not being a necessity).

2. Java file.

3. matlabcontrol 4.0 (You can get it from here )

Getting started:

First, let me explain what matlabcontrol is, and then we shall jump into coding.

matlabcontrol is a Java API that allows for calling MATLAB from Java. Interaction can be performed from either inside MATLAB or outside MATLAB (Of which, I will be discussing outside matlab).

The basic usage pattern with matlabcontrol is

  • to create a factory
  • to create a proxy. (The proxy is used to communicate with MATLAB).

To begin with, you need to add the matlabcontrol 4.0. jar file into your program.

If you’re using Eclipse, you may want to do the following:

Once you create a Java project,

  • right click on your project(displayed on the left part of your screen)
  • Build Path – > Add External Archives
  • Browse the path to locate your .jar file and add it.

Let’s code now!

The code follows the steps mentioned above with required comments.
import matlabcontrol.*;

public class InvokeMatlab {

public static void main(String[] args)
throws MatlabConnectionException, MatlabInvocationException
{
// create proxy
MatlabProxyFactoryOptions options =
new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true).setHidden(true)
.build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
MatlabProxy proxy = factory.getProxy();

// call builtin function
proxy.eval(“disp(‘hello world’)”);

// call user-defined function (must be on the path)
proxy.eval(“addpath(‘F:\\Matlab’)”);
proxy.feval(“Sample”);
proxy.eval(“rmpath(‘F:\\Matlab’)”);

//Set a variable, add to it, retrieve it, and print the result
proxy.setVariable(“a”, 5);
proxy.eval(“a = a + 6”);
double result = ((double[]) proxy.getVariable(“a”))[0];
System.out.println(“Result: ” + result);
proxy.exit();

// close connection
proxy.disconnect();

}
}

Advertisements

One thought on “Invoke Matlab from Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s