Pass MATLAB Structure from Java to MATLAB
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I am working with the Java external language interface [1] in MATLAB R2021b Update 2.
In detail, I am creating a MATLAB structure in Java as described in the documentation [2,3]. This MATLAB structure should be made available in MATLAB without additional data type conversion. Therefore, I am using the following Java code, which is compiled with OpenJDK 1.8 into a JAR-file.
import com.mathworks.matlab.types.Struct;
class Test {
public static Struct getStruct() {
Struct s = new Struct("field1", 1, "field2", 2);
return s;
}
}
If I am calling the Java function Test.getStruct in MATLAB, I obtain the following result:
% Add JAR-file with 'javaaddpath'.
>> s = Test.getStruct();
>> s
s =
com.mathworks.matlab.types.Struct@680afcf0
>> disp(s.field1);
Unrecognized function or variable 'field1'.
This is somehow puzzling me, because I was expecting the variable s to be
>> s = Test.getStruct();
>> s
s =
struct with fields:
field1: 1
field2: 2
>> disp(s.field1);
1
according to the Java data type conversions in the documentation [4].
Does somebody know, what I am doing wrong in my example?
Thx for your help,
db
References:
0 comentarios
Respuestas (1)
Saarthak Gupta
el 28 de Nov. de 2023
Editada: Saarthak Gupta
el 29 de Nov. de 2023
Hi,
I understand you are trying to convert a Struct object (com.mathworks.matlab.types.Struct) returned by a Java method to a MATLAB struct, and subsequently access its fields.
The method ‘getStruct’ of the ‘Test’ class returns an object of type com.mathworks.matlab.types.Struct which is not the same as a typical MATLAB struct object. They are inherently different types as far as MATLAB is concerned.
MATLAB converts the data returned by a Java method to a valid MATLAB type only in 2 scenarios:
1. MATLAB converts primitive data returned from a Java method into types that best represent the data to the MATLAB language.
2. When a Java method is declared to return data of type java.lang.Object, MATLAB converts its value depending on the actual type returned.
MATLAB does not convert other Java objects to MATLAB types.
com.mathworks.matlab.types.Struct class implements the java.util.Map interface, which cannot be converted to a MATLAB structure. Its field can only be accessed via the ‘get’ function (as specified by the ‘Map’ interface).
However, the object returned by the Java function can be converted to a MATLAB structure if its Java class defines field names. If that is the case, we can use the ‘struct’ function to convert the object data to a MATLAB structure.
Refer to the following example:
Consider the following Java class, which defines fields – ‘field1’ and ‘field2’
public class StructLike {
public int field1;
public int field2;
public StructLike(int field1, int field2) {
this.field1 = field1;
this.field2 = field2;
}
}
In MATLAB, run the following code (after adding the exported JAR to the java classpath)
s = StructLike(1, 2);
sAsStruct = struct(s)
We get the following output:
sAsStruct =
struct with fields:
field1: 1
field2: 2
Please refer to the following MATLAB documentation for further reference:
0 comentarios
Ver también
Categorías
Más información sobre Call Java from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!