Joonbum Joonbum - 1 month ago 6
Java Question

How to call Matlab functions from Java using matlabcontrol without starting new session every time?

I am using matlabcontrol to call Matlab functions from Java

My code works well, but it opens new Matlab session whenever I run my code in Java.
I edited according to Javadoc, but I guess I missed something.

I also tried this with/without 'proxy.disconnect()', but it still opens new Matlab session.
Any hint will be very helpful. I am stuck..

below is my Java code

import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;
import matlabcontrol.MatlabProxy;
import matlabcontrol.MatlabProxyFactory;
import matlabcontrol.MatlabProxyFactoryOptions;

public class callSaliencymap {

/**
* @param args
*/
public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
{
//Create a proxy, which we will use to control MATLAB
MatlabProxyFactory factory = new MatlabProxyFactory();
MatlabProxy proxy = factory.getProxy();

MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true)
.setHidden(true)
.build();

factory = new MatlabProxyFactory(options);




//Create proxy factory


//test the saliencymap
Object[] saliencylocation = proxy.returningFeval("runActivation",1,"tank.bmp",1);
System.out.println(saliencylocation[0]);

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

Answer

This is what I did, hope it helps

MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
    .setUsePreviouslyControlledSession(true)
    .setHidden(true)
    .setMatlabLocation(null).build(); 

    MatlabProxyFactory factory = new MatlabProxyFactory(options);
    MatlabProxy proxy = factory.getProxy();