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'.
5
  • Probably need single quotes for the argument. What happens if you try import MyFunction.*? Commented May 2, 2016 at 18:27
  • it imports it but it can't recognize the class Commented May 2, 2016 at 18:32
  • I tried to follow the answer of macduff here 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' Commented May 2, 2016 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. Commented May 2, 2016 at 19:57
  • It's the same version, you can see my question after editing it Commented May 3, 2016 at 18:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Should you not call obj = MyFunction . What is TestFunction ?
Sorry I just changed the test function with another
did you follow option 1 ? add testfunction.class with package directory to your matlab cp ?
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
It's bizarre that there isn't something more convenient than this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.