Main Content

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.Structclass enables you to:

  • 创建一个Structin Java and pass it to MATLAB.

  • 创建一个MATLABstructand return it to Java.

Thecom.mathworks.matlab。types.Structclass implements thejava.util.Mapinterface. However, you cannot change the mappings, keys, or values of aStructreturned from MATLAB.

PassStructtoMATLABFunction

The MATLABsetfunction sets the properties of MATLAB graphics objects. To set several properties in one call toset, it is convenient to use a MATLABstruct. Define thisstructwith 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 MATLABplotfunction.

  • Return the MATLAB handle object to Java as acom.mathworks.matlab。types.HandleObject.

  • 创建一个com.mathworks.matlab。types.Structusing property names and values.

  • 创建一个MATLAB graph and display it for5秒。

  • Pass theHandleObjectand theStructto the MATLABsetfunction usingfeval. This function changes the color and line width of the plotted data.

  • Export the plot to thejpegfile namedmyPlotand 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 MATLABaxesfunction creates axes for a graph and returns a handle object reference. The MATLABgetfunction, when called with one output, returns a MATLABstructwith the properties of the graphics object.

This example code:

  • Creates a MATLAB graphics object and returns the object handle as aHandleObject.

  • Creates a MATLAB structure containing the properties and values of the graphics object and returns it as aStruct.

  • Gets the value of theFontNameproperty from theStruct.

  • Attempts to change the value of theFontNamekey, which throws anUnsupportedOperationExceptionbecause theStructis 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(); } }

Related Topics