Main Content

Using MATLAB Structures in Java

MATLAB Structures

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 form structName.fieldName. The class of a MATLAB structure is struct.

The Java® com.mathworks.matlab.types.Struct class enables you to:

  • Create a Struct in Java and pass it to MATLAB.

  • Create a MATLAB struct and return it to Java.

The com.mathworks.matlab.types.Struct class implements the java.util.Map interface. However, you cannot change the mappings, keys, or values of a Struct returned from MATLAB.

Pass Struct to MATLAB Function

The MATLAB set function sets the properties of MATLAB graphics objects. To set several properties in one call to set, it is convenient to use a MATLAB struct. Define this struct 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.

  • Create a com.mathworks.matlab.types.Struct using property names and values.

  • Create a MATLAB graph and display it for 5 seconds.

  • Pass the HandleObject and the Struct to the MATLAB set function using feval. This function changes the color and line width of the plotted data.

  • Export the plot to the jpeg file named myPlot 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 from MATLAB

The MATLAB axes function creates axes for a graph and returns a handle object reference. The MATLAB get function, when called with one output, returns a MATLAB struct 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 the Struct.

  • Attempts to change the value of the FontName key, which throws an UnsupportedOperationException because the Struct 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();
    }
}

Related Topics