External Interfaces |

**Converting Objects to MATLAB Data Types**

With the exception of objects of class `String`

and class `Object`

, MATLAB does not convert Java objects returned from method calls to a native MATLAB data type. If you want to convert Java object data to a form more readily usable in MATLAB, there are a few MATLAB functions that enable you to do this. These are described in the following sections.

**Converting to the MATLAB double Data Type**

Using the `double`

function in MATLAB, you can convert any Java object or array of objects to the MATLAB `double`

data type. The action taken by the `double`

function depends on the class of the object you specify:

- If the object is an instance of a numeric class (
`java.lang.Number`

or one of the classes that inherit from that class), then MATLAB uses a preset conversion algorithm to convert the object to a MATLAB`double`

. - If the object is not an instance of a numeric class, MATLAB checks the class definition to see if it implements a method called
`toDouble`

. Note that MATLAB uses`toDouble`

to perform its conversion of Java objects to the MATLAB`double`

data type. If such a method is implemented for this class, MATLAB executes it to perform the conversion. - If you are using a class of your own design, you can write your own
`toDouble`

method to perform conversions on objects of that class to a MATLAB`double`

. This enables you to specify your own means of data type conversion for objects belonging to your own classes.

The syntax for the `double`

command is as follows, where `object`

is a Java object or Java array of objects.

**Converting to the MATLAB char Data Type**

With the MATLAB `char`

function, you can convert `java.lang.String`

objects and arrays to MATLAB data types. A single `java.lang.String`

object converts to a MATLAB character array. An array of `java.lang.String`

objects converts to a MATLAB cell array, with each cell holding a character array.

If the object specified in the `char`

command is not an instance of the `java.lang.String`

class, then MATLAB checks its class to see if it implements a method named `toChar`

. If this is the case, then MATLAB executes the `toChar`

method of the class to perform the conversion. If you write your own class definitions, then you can make use of this feature by writing a `toChar`

method that performs the conversion according to your own needs.

The syntax for the `char`

command is as follows, where `object`

is a Java object or Java array of objects.

**Converting to a MATLAB Structure**

Java objects are similar to the MATLAB `structure`

in that many of an object's characteristics are accessible via field names defined within the object. You may want to convert a Java object into a MATLAB `structure`

to facilitate the handling of its data in MATLAB. Use the MATLAB `struct`

function on the object to do this.

The syntax for the `struct`

command is as follows, where `object`

is a Java object or Java array of objects.

The following example converts a `java.awt.Polygon`

object into a MATLAB `structure`

. You can access the fields of the object directly using MATLAB `structure`

operations. The last line indexes into the array, `pstruct.xpoints`

, to deposit a new value into the third array element.

polygon = java.awt.Polygon([14 42 98 124], [55 12 -2 62], 4); pstruct = struct(polygon) pstruct = npoints: 4 xpoints: [4x1 int32] ypoints: [4x1 int32] pstruct.xpoints ans = 14 42 98 124 pstruct.xpoints(3) = 101;

**Converting to a MATLAB Cell Array**

Use the `cell`

function to convert a Java array or Java object into a MATLAB cell array. Elements of the resulting cell array will be of the MATLAB type (if any) closest to the Java array elements or Java object.

The syntax for the `cell`

command is as follows, where `object`

is a Java object or Java array of objects.

In the following example, a MATLAB cell array is created in which each cell holds an array of a different data type. The `cell`

command used in the first line converts each type of object array into a cell array.

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 to cell arrays cellArray = {cell(dblArray), cell(ptArray), cell(strArray)} cellArray = {1x10 cell} {3x1 cell} {2x2 cell} cellArray{1,1} % Array of type double ans = [7] [14] [21] [28] [35] [42] [49] [56] [63] [70] cellArray{1,2} % Array of type Java.awt.Point ans = [1x1 java.awt.Point] [1x1 java.awt.Point] [1x1 java.awt.Point] cellArray{1,3} % Array of type char array ans = 'one' 'two' 'three' 'four'

Java Objects | Introduction to Programming Examples |

© 1994-2005 The MathWorks, Inc.