User`s manual
5 Calling Java from MATLAB
5-52
The three calls to put from the preceding example can be rewritten as
hTable.put(0, 1);
hTable.put(1, 41.287);
hTable.put(2, 'test string');
Passing Objects in an Array
The only types of Java object arrays that you can pass to Java methods are
Java arrays and MATLAB cell arrays.
If the objects have already been placed into an array, either an array returned
from a Java constructor or constructed in MATLAB by the
javaArray function,
then you simply pass it as is in the argument to the method being called. No
conversion is done by MATLAB, as this is already a Java array.
If you have objects that are not already in a Java array, then MATLAB allows
you to simply pass them in a MATLAB cell array. In this case, MATLAB
converts the cell array to a Java array prior to passing the argument.
The following example shows the
mapPoints method of a user-written class
accepting an array of class
java.awt.Point. The method declaration for this is
public Object mapPoints(java.awt.Point p[])
The MATLAB code shown below creates a 2-by-2 cell array containing four
Java Point objects. When the cell array is passed to the
mapPoints method,
MATLAB converts it to a Java array of type
java.awt.Point.
pointObj1 = java.awt.Point(25,143);
pointObj2 = java.awt.Point(31,147);
pointObj3 = java.awt.Point(49,151);
pointObj4 = java.awt.Point(52,176);
cellArray={pointObj1, pointObj2; pointObj3, pointObj4}
cellArray =
[1x1 java.awt.Point] [1x1 java.awt.Point]
[1x1 java.awt.Point] [1x1 java.awt.Point]
testData.mapPoints(cellArray);