User`s manual
5 Calling Java from MATLAB
5-36
This example first creates a scalar MATLAB array, and then successfully
modifies it to be two-dimensional.
matlabArray = 0;
matlabArray(4,5) = 0
matlabArray =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
When you try this with a Java array, you get an error. Similarly, you cannot
create an array of Java arrays from a Java array, and so forth.
javaArray = java.lang.Double(0);
javaArray(4,5) = java.lang.Double(0);
??? Index exceeds Java array dimensions.
Accessing Elements of a Java Array
You can access elements of a Java object array by using the MATLAB array
indexing syntax,
A(row,col). For example, to access the element of array
dblArray located at row 3, column 4, use
row3_col4 = dblArray(3,4)
row3_col4 =
34.0
In Java, this would be dblArray[2][3].
You can also use MATLAB array indexing syntax to access an element in an
object’s data field. Suppose that
myMenuObj is an instance of a window menu
class. This user-supplied class has a data field,
menuItemArray, which is a Java
array of
java.awt.menuItem. To get element 3 of this array, use the following
command.
currentItem = myMenuObj.menuItemArray(3)
Using Single Subscript Indexing to Access Arrays
Elements of a MATLAB matrix are most commonly referenced using both row
and column subscripts. For example, you use
x(3,4) to reference the array