External Interfaces Previous page   Next Page

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:

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.

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.


Previous page  Java Objects Introduction to Programming Examples Next page

© 1994-2005 The MathWorks, Inc.