Accessing Matlab from Java

Java/J2EE COM Interoperability Products Page

This example demonstrates how to access Matlab from Java. J-Integra® for COM is a Java interoperability component that bridges Java and Matlab. It provides bi-directional access of Java objects and COM components.

Contents

  1. Introduction
  2. Run the Java Client on Local Windows Machine
    1. Generate Java Proxies
    2. Create the Example
    3. Compile and Run the example
  3. Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc
  4. More about MATLAB programming

1 Introduction

This example shows you how to programmatically automate MATLAB from Java using the COM API that MATLAB exposes. You can run the Java client on a Windows machine to access its local MATLAB , or run the Java client on a non-Windows machine (such as Linux) to access MATLAB installed on a remote Windows machine.

We assume that you are familiar with Java -- no COM knowledge should be required. We assume you have downloaded and expanded the J-Integra® kit from http://j-integra.intrinsyc.com/ and installed it correctly.

2 Run the Java Client on Local Windows Machine

You can try this example on local Windows machine first to get a feel for how easy it is to use J-Integra® to access MATLAB  from Java. Once you make it working on local machine, you can then try to run the Java client on a non-Windows machine to remotely access MATLAB  on another Windows machine.

2.1 Generate the Java Proxies

Run J-Integra®'s com2java tool on the Windows machine, set Generate Arrays as Objects option (go to Settings, then Options, select Generate Arrays as Objects checkbox, then click OK). Select \MATLABRxx\bin\mlapp.tlb as the type library, choose an empty directory named mlapp (e.g. C:\mlapp) as output directly, and use mlapp as Java package name. Click the Generate Proxies button to generate Java proxies from MATLAB  type library.

The Generate Arrays as Objects option allows you to pass two dimensional arrays.

2.2 Create the Example

Create a .java file named MatlabExample.java. Then copy and paste the Java code below:

public class MatlabExample { 
  public static void main(java.lang.String[] args) throws Exception { 
    try { 
      // DCOM authentication: Make sure NT Domain, NT User, NT Password are valid credentials.
      // Uncomment this line if MatlabExample.java remotely accesses MATLAB :
      // com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD"); 
 
      // Create the MATLAB object
      // Specify host name or IP address of MATLAB machine as parameter if 
      // MatlabExample.java remotely accesses MATLAB. 
      // mlapp.MLApp mlApp = new mlapp.MLApp("123.456.789.0");
      mlapp.MLApp mlApp = new mlapp.MLApp(); 

      String result = mlApp.execute("a = [1 2 3 4; 5 6 7 8;]"); 
      System.out.println("Execute result is " + result); 
      double mreal[][] = new double[2][4]; 
      double mimage[] = new double[0]; 
 
      mlApp.getFullMatrix("a", "base", new Object[]{mreal}, new Object[]{mimage}); 
 
      for (int i = 0; i < 2; i++) { 
        for (int j = 0; j < 4; j++) { 
          System.out.println(mreal[i][j]); 
        } 
      } 
 
    } finally { 
      com.linar.jintegra.Cleaner.releaseAll(); 
    } 
  } 
}

2.3 Compile and Run the Example

On the Java client machine, make sure your CLASSPATH and PATH environment variables are set up according to J-Integra® installation instructions. Compile and run the example in  J-Integra®'s native mode (you need to use DCOM mode if remotely accessing MATLAB ):

javac MatlabExample.java
java -DJINTEGRA_NATIVE_MODE MatlabExample

3 Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc

You can also run the Java client on a remote machine, such as Linux, Solaris, UNIX and AIX. For instance, if you run it on a Linux machine, then you must do the following.:

  1. Move the com2java tool to the Windows machine to generate the Java proxies from MATLAB , and then move the Java proxies from the Windows machine to the Linux machine.
  2. Install J-Integra® (the jintegra.jar file) on the Linux machine and include the jintegra.jar file and generated Java proxies in CLASSPATH.
  3. Use DCOMCNFG to configure MATLAB  on the Windows machine.
  4. Pass correct login credentials to com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD");
    Refer to Configuring DCOM for Remote Access for more information about AuthInfo.setDefault.
  5. Pass the IP address or computer name of the MATLAB machine to the constructor of MLApp object in MatlabExample.java:
    mlapp.MLApp mlApp = new mlapp.MLApp("123.456.789.0");
  6. Move MATLAB Example.java to the Linux machine. Compile and run it in DCOM mode without using DJINTEGRA_NATIVE_MODE property:
    java MatlabExample

4 More about MATLAB programming

We do not provide the documentation of the generated Java proxies since the Java proxies are just mapped from the programming API of the COM component. For more information about MATLAB programming, please consult with The MathWorks, Inc. It's also easier to find a VB example first, and then convert the VB example to Java program using the reference Mapping VB Code to Java Code.