0

I'm trying to call a java function from a Matlab script, I tried all the solutions put in the website but I didn't get an issue. My class is simple:

  package testMatlabInterface;

public class TestFunction
{
  private double value;

  public TestFunction()
  {
      value=0;
  }

  public double Add(double v)
  {
      value += v;
      return value;
  }

  public static void main(String args[])
  {

  }
}

So I put .java file (also .class) in my workingspace C:\scriptsMatlab and I added this path to javaclasspath of Matlab, but when I try to call the function , it tells me that there's no class with this name in javaclasspath of Matlab.

EDIT: Here's the version of java that Matlab uses:

Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode

And this is the version on jdk which I used to compile my class : enter image description here

But when I try to execute this commande from matlab

>> javaaddpath 'C:\scriptsMatlab'
>> obj = TestFunction

it tells me:

Undefined function or variable 'TestFunction'.
Eadhun Di
  • 132
  • 1
  • 13
  • Probably need single quotes for the argument. What happens if you try `import MyFunction.*`? – mhopeng May 02 '16 at 18:27
  • it imports it but it can't recognize the class – Eadhun Di May 02 '16 at 18:32
  • I tried to follow the answer of macduff here http://stackoverflow.com/questions/9520503/calling-java-from-matlab but i get stuck in ' methodsview testMatlabInterface.TestFunction' it says that 'No class testMatlabInterface.TestFunction can be located or no methods for class' – Eadhun Di May 02 '16 at 18:50
  • which java version did you use to compile your class? It's best to use the same major version as your Matlab uses, otherwise it may not work. – nirvana-msu May 02 '16 at 19:57
  • It's the same version, you can see my question after editing it – Eadhun Di May 03 '16 at 18:16

1 Answers1

0

Option 1

  1. Check if same JRE/JDK is being used to compile your JAVA file. Execute on Matlab:

    version -java
    
  2. Compie your MyFunction.java with same jdk as above , and locate your MyFunction.class

  3. Locate your Matlab classpath.txt. Type following into matlab cmd.

    which classpath.txt
    
  4. Open your classpath.txt as administrator.Add the full path for the directory with the MyFunction.class to the end of the 'classpath.txt' file as a single line and save the file.

  5. Restart Matlab.

  6. To run in Matlab . Create object of MyFunction.

    obj = MyFunction
    

    To run main() method in matlab.

    javaMethod('main', obj, '')
    

Option 2

Follow Steps 1-2.

Execute following command in Matlab

JAVAADDPATH PATH/to/Directoryof MyFunction.class.

No need to restart Matlab here. Just run using

obj = MyFunction;
javaMethod('main', obj);

From MathWorks :

javaMethod(MethodName,JavaObj,x1,...,xN) calls the method in the class of the Java® object array with the signature matching the arguments x1,...,xN.

javaMethod(StaticMethodName,ClassName,x1,...,xN) calls the static method in class ClassName.

Community
  • 1
  • 1
Sid
  • 115
  • 1
  • 12
  • Should you not call obj = MyFunction . What is TestFunction ? – Sid May 06 '16 at 20:54
  • Sorry I just changed the test function with another – Eadhun Di May 08 '16 at 17:36
  • did you follow option 1 ? add testfunction.class with package directory to your matlab cp ? – Sid May 08 '16 at 19:45
  • 1
    Classpath is not being added properly . Check javaclasspath. also try following javaaddpath('directory where the .class file resides'); a = TestFunction(); //Creates an object of the java class. methodsview(a); // displays all the methods. a.Add() ; calls the Java method Add(). After you add classpath check classpath.txt of matlab , does it have the cp of .class file. Also remove the .java in the directory. only keep the .class in the classpath – Sid May 08 '16 at 21:01
  • It's bizarre that there isn't something more convenient than this. – Dave C Sep 18 '17 at 18:26