I'm trying to import a custom Java class into matlab. I found this SO Question that I followed.
I have the following Java code
package mypackage.release;
public class TestArgu {
public void addNumber(int aNumber){
ansNumber = aNumber+5;
chk = aNumber;
System.out.println("input number = " + chk + ".\n");
System.out.println("ans = " + ansNumber + ".\n");
}
public int ansChk(){
return ansNumber;
}
private int ansNumber;
private int chk;
}
Then I compile with
javac TestArgu.java
I make sure I add the folder that contains the TestArgu.class
with javaaddpath('.')
file and try to call it with matlab.
>> a = mypackage.release.TestArgu();
Undefined variable "mypackage" or class "mypackage.release.TestArgu".
>> import mypackage.release.*;
>> a = TestArgu();
Undefined function or variable 'TestArgu'.
>> a = mypackage.release.TestArgu.addNumber(1);
Undefined variable "mypackage" or class "mypackage.release.TestArgu.addNumber".
I'm using the same version of java to compile that matlab uses. (JDK 7, Matlab 2013b)
Where am I going wrong ?
I was able to reproduce the same behaviour with the code above. I think one problem is that there is no constructor for this class, so a=TestArgu()
will fail. I suggest doing the following. Change the class definition by removing the package my package.release
statement and adding a constructor to get
public class TestArgu {
public TestArgu()
{
ansNumber = 0;
chk = 0;
}
public void addNumber(int aNumber){
ansNumber = aNumber+5;
chk = aNumber;
System.out.println("input number = " + chk + ".\n");
System.out.println("ans = " + ansNumber + ".\n");
}
public int ansChk(){
return ansNumber;
}
private int ansNumber;
private int chk;
}
Compile the code, as before
$ javac TestArgu.java
And then in MATLAB, add the path to wherever this compiled class exists
>> javaaddpath('/Users/geoff/Development/java'); % or wherever
Then instantiate the object
>> a = TestArgu
a =
TestArgu@2a307bb2
And try out the method
>> a.addNumber(37)
input number = 37.
ans = 42.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.