How can I specify a structure field starting with upper case when working with MATLAB Production Server 1.0 (R2012b) Java client library?

2 views (last 30 days)
The MATLAB code running on my MPS expects a structure with two fields: "field1" and "Field2" as input. It is important that the second field is written with a capital F. I tried to create this structure by implementing the following Java class:
public class MyStruct {
private String field1;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
private double Field2;
public double getField2() {
return Field2;
}
public void setField2(double field2) {
Field2 = field2;
}
}
But I notice that on the MATLAB side both fields in the stuct start with a lower case f.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Oct 2013
When working with Java classes to represent MATLAB structures, it is good to know that the Java Beans Introspector (<http://docs.oracle.com/javase/6/docs/api/java/beans/Introspector.html>) is used to map properties to fields and its default naming conventions are used.
This means that by default its decapitalize method is used:
And thus it would not be possible to define a property which will map to a structure field which starts with an upper case.
However this behavior can be overriden by implementing a BeanInfo class with a custom getPropertyDescriptors() method. For example:
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;
public class MyStructBeanInfo extends SimpleBeanInfo {
@Override
public PropertyDescriptor[] getPropertyDescriptors() {
PropertyDescriptor[] props = new PropertyDescriptor[2];
try {
// field1 uses default naming conventions so we do not need to explicitly specify the accessor names.
props[0] = new PropertyDescriptor("field1",MyStruct.class);
// Field2 uses a custom naming convention so we do need to explicitly specify the accessor names.
props[1] = new PropertyDescriptor("Field2",MyStruct.class,"getField2","setField2");
return props;
} catch (IntrospectionException e) {
e.printStackTrace();
}
return null;
}
}

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2012b

Community Treasure Hunt

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

Start Hunting!