0

I have a MATLAB script running on a Java Eclipse project via the matlabcontrol.jar package.

I have the following set-up

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

MatlabProxyFactory factory = new MatlabProxyFactory(options);

MatlabProxy proxy = factory.getProxy();

//some code invoking the proxy.eval() method

The problem is that I have the MATLAB script running several times over the course of the runtime of the simulation. How can I make it so that Java doesn't have to reconnect with MATLAB every single time I want to use the MATLAB function?

Any help would be appreciated.

Thanks!

Yousef Hindy
  • 73
  • 1
  • 1
  • 6

1 Answers1

1

I'm not expert on JAVA, so I will give my solution in simple words that you can translate to JAVA world. :)

  1. Create a JAVA singleton class which is responsible for handling connections.
  2. Provide a public static read-only property pointing to singleton object.
  3. Now use singleton object to call MATLAB functions.

E.g.:

public class MATLABConnector
{
      private MATLABConnector con=new MATLABConnector();
      MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
        .setUsePreviouslyControlledSession(true)
        .setHidden(true)
        .setMatlabLocation(null).build();

      MatlabProxyFactory factory = new MatlabProxyFactory(options);

      private MATLABConnector() 
      {
            // Do basic initializations.
      }        

      private boolean checkConnecionStatus();
      private boolean establishConnection();
      public static MATLABProxy getProxy()
         {
              if(!con.checkConnectionStatus())
                    con.establishConnection();
              return con.factory.getProxy();
         }
  }