Accessing Matlab from Java |
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.
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.
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.
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.
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(); } } } |
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
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.:
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.