Example of coding a `struct` in Java when packaging Matlab function?
9 次查看(过去 30 天)
显示 更早的评论
I am embarking on writing a Java wrapper for intlinprog, and learning Java at the same time. My OOP experience is C++ circa 2004, so it has been an interesting learning curve reading up on Java code idioms, patterns, Matlab data conversions, the packaging process itself, and the Java compilation/invocation.
intlinprog takes a struct argument named options. Is there an example of how to code up a MATLAB struct in Java? According to Rules for Data Conversion Between Java and MATLAB, "Structure arrays are constructed and accessed as arrays of MWArray". Most examples of Java/Matlab interoperation use classes derived from MWArray.
I am using Matlab Compiler SDK rather than the Matlab engine, so I assume that Java class com.mathworks.matlab.types.Struct does not apply. I welcome any corrections if I'm wrong.
0 个评论
采纳的回答
Todd Flanagan
2020-10-29
编辑:Todd Flanagan
2020-10-29
The eaiest way to learn how to create structs in the Java API is to add a sample in the Library Compiler tool with a sample struct and sample data. This will generate Java code that will show you how to use the struct api with your own data.
Here is an example:
a.i1 = 1.0
a.i2 = 'a string'
foo(a)
And the generated Java method:
public static void fooExample() {
MWStructArray aIn = null;
MWArray aIn_i1_1 = null;
MWArray aIn_i2_1 = null;
Object[] results = null;
try {
// instantiate data within struct aIn
double aIn_i1_1Data = 1.0;
aIn_i1_1 = new MWNumericArray(aIn_i1_1Data, MWClassID.DOUBLE);
String aIn_i2_1Data = "a string";
aIn_i2_1 = new MWCharArray(aIn_i2_1Data);
// instantiate struct aIn and set inner data
int[] aInDims = {1, 1};
String[] aInFields = {"i1", "i2"};
aIn = new MWStructArray(aInDims, aInFields);
aIn.set("i1", 1, aIn_i1_1);
aIn.set("i2", 1, aIn_i2_1);
results = class1Instance.foo(aIn);
} catch (Exception e) {
e.printStackTrace();
} finally {
// Dispose of native resources
MWArray.disposeArray(aIn);
MWArray.disposeArray(aIn_i1_1);
MWArray.disposeArray(aIn_i2_1);
MWArray.disposeArray(results);
}
}
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Java Package Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!