IncorrectJavaData Types
JavaString toMATLABCharacter Vector
Struct of Character Vectors
Some MATLAB®functions accept a结构体
of name-value pairs as input arguments. The MATLAB Engine API for Java®provides thecom.mathworks.matlab.engine.Struct
class to create this data structure in Java and pass it to MATLAB, where it is converted to a MATLAB结构体
.
Some MATLAB functions that accept结构体
input require field values to be MATLAB character vectors (char
) instead of MATLAB strings (string
). To create a JavaStruct
with the correct type of values, convert fromString
tochar
array before passing the variable to MATLAB.
You can use thetoCharArray
method for the conversion:
char[] on = "on".toCharArray(); char[] yOut = "yOut".toCharArray(); char[] tOut = "tOut".toCharArray(); Struct simParam = new Struct("SaveOutput", on, "OutputSaveName", yOut,"SaveTime", on, "TimeSaveName", tOut);
String Argument to Character Vector
When MATLAB functions requirechar
inputs, you can convert the JavaString
in the function call passed to MATLAB. For example, the MATLABeval
function requireschar
input:
double result = engine.feval("eval", "3+5");Undefined function 'eval' for input arguments of type 'string'..
Passing achar
array works correctly.
double result = engine.feval("eval", "3+5".toCharArray());
Setting Graphics Object Properties fromJava
You can set the values of MATLAB graphics object properties using the handle of the object. Pass the property names and property values as Javachar
arrays when passing to MATLAB functions.
double[][] data = {{1.0, 2.0, 3.0}, {-1.0, -2.0, -3.0}}; HandleObject[] h = eng.feval("plot", (Object)data); String property = ("HitTest"); String value = ("off"); eng.feval(0,"set", h, property.toCharArray(), value.toCharArray());
Java整数MATLABdouble
Some MATLAB functions, such assqrt
restrict the input todouble
orsingle
precision values. The MATLAB engine converts Java integers to MATLABint32
values. For MATLAB functions that do not accept integer values, ensure that you pass appropriate numeric values.
double result = engine.feval("sqrt", 4);Undefined function 'sqrt' for input arguments of type 'int32'.
通过双工作正常.
double result = engine.feval("sqrt", 4.0);