Handle Data Returned from Java Methods - MATLAB & Simulink (original) (raw)

If a Java® method returns a primitive data type, then MATLAB® converts the data, as shown in the table in Primitive Return Types.

If a Java method signature specifies a return data of typejava.lang.Object, then MATLAB converts the actual type returned, as shown in the table in java.lang.Object Return Types.

MATLAB does not convert other Java objects to MATLAB types. For information about handling this data, see Functions to Convert Java Objects to MATLAB Types.

Primitive Return Types

MATLAB converts primitive data returned from a Java method into types that best represent the data to the MATLAB language. This table shows how MATLAB converts the data. For some Java types, MATLAB treats scalar and array returns differently.

Java Return Type Resulting MATLAB Type — Scalar Resulting MATLAB Type — Array
boolean logical logical
byte double int8
short double int16
int double int32
long double int64
float double single
double double double
char char char

Example

The signature for the java.lang.String methodtoCharArray is:

public char[] toCharArray()

Call the method on a String object. MATLAB converts the output to a char array.

str = java.lang.String('hello'); res = str.toCharArray'

res =

1×5 char array

hello

java.lang.Object Return Types

When a Java method is declared to return data of type java.lang.Object, MATLAB converts its value depending on the actual type returned. This table shows how MATLAB converts the data.

Actual Java Type Resulting MATLAB Type — Scalar
java.lang.Boolean logical
java.lang.Byte double
java.lang.Short double
java.lang.Integer double
java.lang.Long double
java.lang.Float double
java.lang.Double double
java.lang.Character char
java.lang.String char

There is no conversion if the return argument is a subclass of Object or an array of Object. The object remains a Java object. However, if you index into a returned Object array, MATLAB converts the value according to the table. For more information, see Converting Object Array Elements to MATLAB Types.

Example

Refer to the following signature for a getData method.

java.lang.Object getData()

If getData returns a java.lang.Integer object, then MATLAB converts the value to double.

Functions to Convert Java Objects to MATLAB Types

MATLAB only converts object data return values if the method signature specifiesjava.lang.Object. If the signature specifies any other object type, then MATLAB does not convert the value. For example, MATLAB does convert the return value for this method signature:

java.lang.Object getData()

But MATLAB does not convert the return value for this method:

java.lang.String getData()

To convert Java object data to MATLAB data, use MATLAB functions as described in these topics:

Convert to MATLAB Numeric Types

To convert Java numeric types to MATLAB types, use a numeric MATLAB function like double. The action taken by thedouble function depends on the class of the object you specify.

Note

If the class of the object is not derived from java.lang.Number and it does not implement a toDouble method, then thedouble function displays an error message.

Convert to MATLAB Strings

To convert java.lang.String objects and arrays to MATLAB strings or character vectors, use the MATLABstring or char function.

If the object specified in the MATLAB function is not an instance of the java.lang.String class, then MATLAB checks its class definition for a toString or atoChar method. If you create your own class, then write atoString or toChar method to specify the string conversion.

Note

If the class of the object is not java.lang.String and it does not implement a toChar method, then the char function displays an error message.

Convert to MATLAB Structure

If a Java class defines field names, then use the struct function to convert the object data to a MATLAB structure.

Suppose that you call a Java method that returns a java.awt.Polygon object. The class defines fields xpoints and ypoints. To run this example, create a polygon variable.

polygon = java.awt.Polygon([14 42 98 124],[55 12 -2 62],4);

Convert the object to a structure and display the x,y coordinates for the third point.

pstruct = struct(polygon)

pstruct =

struct with fields:

npoints: 4
xpoints: [4×1 int32]
ypoints: [4×1 int32]

Convert to MATLAB Cell Array

If your Java methods return different types of data, then use thecell function to convert the data to MATLAB types. Elements of the resulting cell array are converted according to thePrimitive Return Types and java.lang.Object Return Types tables.

Suppose that you call Java methods that return arguments of type java.lang.Double,java.awt.Point, and java.lang.String. To run this example, create variables of these types.

import java.lang.* java.awt.*

% Create a Java array of double dblArray = javaArray('java.lang.Double',1,10); for m = 1:10 dblArray(1,m) = Double(m * 7); end

% Create a Java array of points ptArray = javaArray('java.awt.Point',3); ptArray(1) = Point(7.1,22); ptArray(2) = Point(5.2,35); ptArray(3) = Point(3.1,49);

% Create a Java array of strings strArray = javaArray('java.lang.String',2,2); strArray(1,1) = String('one'); strArray(1,2) = String('two'); strArray(2,1) = String('three');
strArray(2,2) = String('four');

Convert each array to a cell array. You can use cellArray in MATLAB functions.

cellArray = {cell(dblArray),cell(ptArray),cell(strArray)}

cellArray =

1×3 cell array

{1×10 cell}    {3×1 cell}    {2×2 cell}

Each cell holds an array of a different type. Display the contents.

cellArray{1,1} % Array of type double

ans =

1×10 cell array

[7]    [14]    [21]    [28]    [35]    [42]    [49]    [56]    [63]    [70]

cellArray{1,2} % Array of type Java.awt.Point

ans =

3×1 cell array

[1×1 java.awt.Point]
[1×1 java.awt.Point]
[1×1 java.awt.Point]

cellArray{1,3} % Array of type char array

ans =

2×2 cell array

'one'      'two' 
'three'    'four'

See Also

Topics