Main Content

Data Conversion Between Java and MATLAB

Overview

The call signature for a method that encapsulates a MATLAB® function uses one of the MATLAB data conversion classes to pass arguments and return output. When you call any such method, all input arguments not derived from one of the MWArray classes are converted by the compiler to the correct MWArray type before being passed to the MATLAB method.

For example, consider the following Java® statement:

result = theFourier.plotfft(3, data, new Double(interval));

The third argument is of type java.lang.Double, which converts to a MATLAB 1-by-1 double array.

See Rules for Data Conversion Between Java and MATLAB for a complete list of rules to convert between Java and MATLAB Compiler SDK™ data types.

Call MWArray Methods

The conversion rules apply not only when calling your own methods, but also when calling constructors and factory methods belonging to the MWArray classes. For example, the following code calls the constructor for the MWNumericArray class with a Java double input. The MATLAB Compiler SDK product converts the Java double input to an instance of MWNumericArray having a ClassID property of MWClassID.DOUBLE. This is the equivalent of a MATLAB 1-by-1 double array.

double Adata = 24;
MWNumericArray A = new MWNumericArray(Adata);
System.out.println("Array A is of type " + A.classID());

When you run this example, the results are as follows:

Array A is of type double

Specifying the Type

To specify the MATLAB to Java type conversion, you supply a specific data type in the constructor. The MATLAB Compiler SDK product converts to the specified type rather than following the default conversion rules.

The following code specifies that A should be constructed as a MATLAB 1-by-1 16-bit integer array:

double Adata = 24;
MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT16);

Create Buffered Images from a MATLAB Array

Use the renderArrayData method to:

  • Create a buffered image from data in a given MATLAB array.

  • Verify the array is of three dimensions (height, width, and color component).

  • Verify the color component order is red, green, and blue.

    Search on renderArrayData in the Javadoc for information on input parameters, return values, exceptions thrown, and examples. The Javadoc is located at matlabroot/help/javabuilder/MWArrayAPI.