Main Content

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.Structclass 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 JavaStructwith the correct type of values, convert fromStringtochararray before passing the variable to MATLAB.

You can use thetoCharArraymethod 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 requirecharinputs, you can convert the JavaStringin the function call passed to MATLAB. For example, the MATLABevalfunction requirescharinput:

double result = engine.feval("eval", "3+5");Undefined function 'eval' for input arguments of type 'string'..

Passing achararray 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 Javachararrays 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 assqrtrestrict the input todoubleorsingleprecision values. The MATLAB engine converts Java integers to MATLABint32values. 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);

Related Topics