User`s manual

5 Calling Java from MATLAB
5-34
Creating an Array of Objects Within MATLAB
To call a Java method that has one or more arguments defined as an array of
Java objects, you must, under most circumstances, pass your objects in a Java
array. You can construct an array of objects in a call to a Java method or
constructor. Or you can create the array within MATLAB.
The MATLAB
javaArray function lets you create a Java array structure that
can be handled in MATLAB as a single multidimensional array. You specify
the number and size of the array dimensions along with the class of objects you
intend to store in it. Using the one-dimensional Java array as its primary
building block, MATLAB then builds an array structure that satisfies the
dimensions requested in the
javaArray command.
Using the javaArray Function
To create a Java object array, use the MATLAB javaArray function, which has
the following syntax.
A = javaArray('element_class', m, n, p, ...)
The first argument is the ’element_class’ string, which names the class of the
elements in the array. You must specify the fully qualified name (package and
class name). The remaining arguments (
m, n, p, ...) are the number of
elements in each dimension of the array.
An array that you create with
javaArray is equivalent to the array that you
would create with the Java code.
A = new element_class[m][n][p]...;
The following command builds a Java array of four lower level arrays, each
capable of holding five objects of the
java.lang.Double class. (You will
probably be more likely to use primitive types of
double than instances of the
java.lang.Double class, but in this context, it affords us a simple example.)
dblArray = javaArray('java.lang.Double', 4, 5);
The javaArray function does not deposit any values into the array elements
that it creates. You must do this separately. The following MATLAB code
stores objects of the
java.lang.Double type in the Java array dblArray that
was just created.