facebook youtube pinterest twitter reddit whatsapp instagram

Calling Matlab from Java

If you are developing a Java program that needs to handle large amounts of data and perform difficult calculations with it or you need to manipulate matrices without having to code these functions in Java, you can use the almighty power of Matlab to perform these operations in a much more efficient way.

There are several ways to access Matlab from Java, and you can also write Java programs in Matlab. But if you need to develop your program in Java, and you want to be able to call Matlab functions from your program without having to go to Matlab, the easiest solution is to use MatlabControl.

Matlab control

Matlab control is an API that allows you to call Matlab commands from Java. There are four main functions:

    • MatlabControl.eval(String command): the string command is evaluated as if it was executed from the Matlab command prompt.
    • MatlabControl.feval(String command, Object[] args): similar to the first one, but used for functions which need arguments. If the function takes n arguments, then args should be an array of size n.
    • Object[] MatlabControl.returningEval(String command, int returnCount): command is evaluated and it returns to the Java program returnCount values.
    • Object[] MatlabControl.returningFeval(String command, Object[] args, int returnCount): again, you pass a string that will be evaluated in Matlab with its arguments, and the number of return arguments.

Adding MatlabControl to your Java project

To start using these functions, the first thing you need to do is to add the latest MatlabControl jar to your project. You will find this jar here. If you are using Eclipse, go to Project – Properties, select Java Build Path and in the Libraries tab click on “Add External JARs” to add the jar you have just downloaded.

Controlling Matlab

Create a new Java class, and import the matlabcontrol package. To control Matlab from this class, you need to create a proxy upon which the three functions listed above will be called. Once you have finished using Matlab, you must call disconnect().

public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
{
    //Create a proxy
    MatlabProxyFactory factory = new MatlabProxyFactory();
    MatlabProxy proxy = factory.getProxy();

    //Execute Matlab commands.

    //Disconnect the proxy
    proxy.disconnect();
}

Variables in Matlab

You can set variables in the Matlab environment, manipulate them, and then obtain its value.

public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
{
    //Create a proxy
    MatlabProxyFactory factory = new MatlabProxyFactory();
    MatlabProxy proxy = factory.getProxy();

    proxy.setVariable("x", 1);
    proxy.eval("x = x + 1");
    double result = ((double[]) proxy.getVariable("x"))[0];

    //Disconnect the proxy
    proxy.disconnect();
}

In the above code, we set a new variable x, add 1 to it, and then get its value. The way to retrieve its value may seem odd to you, and that’s because Matlab’s numeric values are always arrays, even if there is only one value. Therefore, it will always return an Object array, so you need to cast it to double[], and get the first value.

returningEval

When you use this function, Matlab will return an array with the size you passed in the second argument. Therefore, you have an object array with more Object[] inside. If you only expect one argument, then you need to get its first value, and then cast it and index it the same way you did before:

public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
{
    //Create a proxy
    MatlabProxyFactory factory = new MatlabProxyFactory();
    MatlabProxy proxy = factory.getProxy();

    proxy.eval("A = [3 3]");

    Object[] arguments = proxy.returningEval("size(A,1)", 1);
    Object firstArgument = arguments[0];
    double innerValue = ((double[]) firstArgument)[0];
    System.out.println("Result: " + innerValue);

    //Disconnect the proxy
    proxy.disconnect();
}

returningFeval

This function is very similar, but now you need to pass the arguments when calling the function:

public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
{
    //Create a proxy
    MatlabProxyFactory factory = new MatlabProxyFactory();
    MatlabProxy proxy = factory.getProxy();

    Object[] arguments = proxy.returningFeval('sqrt',5,1);
    Object firstArgument = arguments[0];
    double innerValue = ((double[]) firstArgument)[0];
    System.out.println("Result: " + innerValue);

    //Disconnect the proxy
    proxy.disconnect();
}

Conclusion

As you can see, calling Matlab from your Java program is really easy. Now you can benefit from all of the Matlab functions to perform complicated operations and handle large matrices. If you have any doubts, you know what to do!