How do I get a value returned by a Java method in MATLAB?

4 Ansichten (letzte 30 Tage)
Hi,
I am trying to get value form a java class method. My Java code is as follows:
public class HelloWorld {
public static void main( String args[] )
{
System.out.println( "Hello World:" );
if (args.length >0){
System.out.println( args[0] );
}
}
public int f1 (String args[]){
if (args.length >0){
System.out.println( args[0] );
}
return 123;
}
}
When I try to execute the f1 method, it works as long as I am not attempting to get the value 123 to a MATLAB variable. What am I doing wrong? How do I fix this?
Trial>> o=HelloWorld
o =
HelloWorld@4a8b5227
Trial>> javaMethod('f1',o,'ABC')
ABC
Trial>> o.f1('ABC')
ABC
Trial>> a=javaMethod('f1',o,'ABC')
ABC
One or more output arguments not assigned during call to "javaMethod".
Trial>> a=o.f1('ABC')
ABC
One or more output arguments not assigned during call to "f1".
Trial>>

Akzeptierte Antwort

Bharath Lohray
Bharath Lohray am 28 Okt. 2015
Bearbeitet: Bharath Lohray am 28 Okt. 2015
Never mind. I was iterating development and had the old class in memory. I had executed
clear classes
between tries, but I got my numbers when I executed
clear java
Here, I have the old implementation (returns 123) in memory.
Trial>> o.f1('ABC')
ABC
ans =
123
Then I change it to return 121
Trial>> clear classes
Trial>> o=HelloWorld
o =
HelloWorld@38d5b107
Trial>> o.f1('ABC')
ABC
ans =
123
Trial>>
To make this work, I executed `clear java`
Trial>> clear java
Trial>> javaaddpath .
Trial>> o=HelloWorld
o =
HelloWorld@273c947f
Trial>> o.f1('ABC')
ABC
ans =
121
Trial>>
Voilà!
I expected clear classes to work as after running `clear classes`, 'HelloWorld' ceased to show up in the output of `inmem`

Weitere Antworten (0)

Kategorien

Mehr zu Call Java from MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by