UsingMATLABStructures inJava
MATLABStructures
MATLAB®structures contain data and references it using field names. Each field can contain any type of data. MATLAB code accesses data in a structure using dot notation of the formstructName.fieldName
. The class of a MATLAB structure isstruct
.
The Java®com.mathworks.matlab。types.Struct
class enables you to:
创建一个
Struct
in Java and pass it to MATLAB.创建一个MATLAB
struct
and return it to Java.
Thecom.mathworks.matlab。types.Struct
class implements thejava.util.Map
interface. However, you cannot change the mappings, keys, or values of aStruct
returned from MATLAB.
PassStruct
toMATLABFunction
The MATLABset
function sets the properties of MATLAB graphics objects. To set several properties in one call toset
, it is convenient to use a MATLABstruct
. Define thisstruct
with field names that match the names of the properties that you want to set. The value referenced by the field is the value assigned the property.
This example code performs the following steps:
Start MATLAB.
Pass a double array to the MATLAB
plot
function.Return the MATLAB handle object to Java as a
com.mathworks.matlab。types.HandleObject
.创建一个
com.mathworks.matlab。types.Struct
using property names and values.创建一个MATLAB graph and display it for
5
秒。Pass the
HandleObject
and theStruct
to the MATLABset
function usingfeval
. This function changes the color and line width of the plotted data.Export the plot to the
jpeg
file namedmyPlot
and closes the engine connection.
import com.mathworks.engine.*; import com.mathworks.matlab.types.*; public class CreateStruct { public static void main(String[] args) throws Exception { MatlabEngine eng = MatlabEngine.startMatlab(); double[] y = {1.0, 2.0, 3.0, 4.0, 5.0}; HandleObject h = eng.feval("plot", y); eng.eval("pause(5)"); double[] color = {1.0, 0.5, 0.7}; Struct s = new Struct("Color", color, "LineWidth", 2); eng.feval("set", h, s); eng.eval("print('myPlot', '-djpeg')"); eng.close(); } }
Get Struct fromMATLAB
The MATLABaxes
function creates axes for a graph and returns a handle object reference. The MATLABget
function, when called with one output, returns a MATLABstruct
with the properties of the graphics object.
This example code:
Creates a MATLAB graphics object and returns the object handle as a
HandleObject
.Creates a MATLAB structure containing the properties and values of the graphics object and returns it as a
Struct
.Gets the value of the
FontName
property from theStruct
.Attempts to change the value of the
FontName
key, which throws anUnsupportedOperationException
because theStruct
is unmodifiable.
import com.mathworks.engine.*; import com.mathworks.matlab.types.*; public class GetStruct { public static void main(String[] args) throws Exception { MatlabEngine eng = MatlabEngine.startMatlab(); HandleObject h = eng.feval("axes"); Struct s = eng.feval("get", h); Object fontName = s.get("FontName"); System.out.println("The font name is " + fontName.toString()); try { s.put("FontName", "Times"); }catch(UnsupportedOperationException e){ e.printStackTrace(); } eng.close(); } }