How to Call a Java Class from Matlab

General topics that don't fit into other forum categories

How to Call a Java Class from Matlab

Postby tunc » 03 May 2015, 20:34

Following steps demonstrate how you can call the methods of a third party java class from Matlab, with a simple example (MyMathLib.java) that you can try yourself. This example also demonstrates how simple standard variables (as method arguments) can be passed from Matlab to java, and vice versa.

For more general information about Matlab and java integration you may visit: Bringing Java Classes into MATLAB Workspace

1) Compile the code you have written in java, with backwards compatibility if necessary. Find the example code (MyMathLib.java) at the bottom of this page.
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. javac MyMathLib.java
GeSHi ©

You may get the java version supported by your Matlab installation with the following command in Matlab:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. % get java version
  2. version -java
GeSHi ©

If the current version of java on your computer is not supported by Matlab you will need to compile the java code with backwards compatibility:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. javac -source 1.6 -target 1.6 MyMathLib.java
GeSHi ©


2) Add the path to the directory of your java code to classpath.txt file of Matlab.
You may find the file classpath.txt with following command in Matlab:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. which classpath.txt
GeSHi ©

Open classpath.txt with a text editor as administrator, and add the complete path to the file, like:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. C:\Users\John\Documents\MyJavaCode
GeSHi ©


3) Restart Matlab

4) First create the object instance, then call its methods with following commands in Matlab:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. % create the object instance in Matlab
  2. lib = MyMathLib;
  3. % call first method of the class: Simple scalar addition
  4. AddNumbers(lib, 4, 5)
  5. % call second method of the class: Transpose matrix
  6. a = rand(2,4)
  7. TransposeMatrix(lib, a)
  8. % call third method of the class: Matrix Multiplication
  9. a = rand(2,4);
  10. b = rand(4,3);
  11. c = MultiplyMatrices(lib, a, b)
GeSHi ©

For the first method, simple scalar variables of type double are passed from Matlab to java, and vice versa. For the second and third methods, array variables of type double[][] (i.e. Matlab matrix) are passed from Matlab to java, and vice versa.

Example Java Code (MyMathLib.java)
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // My Math Lib in Java (2. May 2015)
  2. public class MyMathLib {
  3.  
  4.     public static double AddNumbers(double x, double y) {
  5.        return x + y;
  6.     }
  7.    
  8.     public static double[][] TransposeMatrix(double[][] M)
  9.     {
  10.         int i, j;
  11.         double x;
  12.         double[][] R = new double[M[0].length][M.length];
  13.        
  14.         for (i=0; i < M[0].length; i++)
  15.         {
  16.             for (j=0; j < M.length; j++)
  17.             {
  18.                 x = M[j][i];
  19.                 R[i][j] = x;
  20.             }
  21.         }
  22.         return R;
  23.     }
  24.    
  25.     public static double[][] MultiplyMatrices(double[][] M, double[][] N) throws Exception
  26.     {
  27.         if (M == null || N == null || M[0] == null || N[0] == null)
  28.             throw new Exception("Null-valued or empty input matrix!");
  29.         if ( M[0].length != N.length)
  30.             throw new Exception("Column count of first input matrix must be equal to the row count of second input matrix!");
  31.            
  32.         int rowM = M.length;
  33.         int colM = M[0].length;
  34.         int rowN = N.length;
  35.         int colN = N[0].length;
  36.  
  37.         double[][] R = new double[rowM][colN];
  38.         double sum = 0.0;
  39.         int i,j,k;
  40.        
  41.         for ( i = 0 ; i < rowM ; i++ )
  42.         {
  43.             for ( j = 0 ; j < colN ; j++ )
  44.             {  
  45.                for ( k = 0 ; k < rowN ; k++ )
  46.                {
  47.                   sum = sum + M[i][k] * N[k][j];
  48.                }
  49.                R[i][j] = sum;
  50.                sum = 0;
  51.             }
  52.         }
  53.         return R;
  54.     }
  55. }
  56.  
GeSHi ©

Tunc Ali Kütükcüoglu
tunc
 
Posts: 25
Joined: 22 Jul 2014, 19:29
Location: Switzerland

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron