User`s manual
5 Calling Java from MATLAB
5-48
Passing Built-In Data Types
Java has eight data types that are intrinsic to the language and are not
represented as Java objects. These are often referred to as built-in, or
elemental, data types and they include
boolean, byte, short, long, int,
double, float, and char. MATLAB converts its own data types to these Java
built-in types according to the table, “Conversion of MATLAB Types to Java
Types” on page 5-47. Built-in types are in the first 10 rows of the table.
When a Java method you are calling expects one of these data types, you can
pass it the type of MATLAB argument shown in the left-most column of the
table. If the method takes an array of one of these types, you can pass a
MATLAB array of the data type. MATLAB converts the data type of the
argument to the type assigned in the method declaration.
The MATLAB code shown below creates a top-level window frame and sets its
dimensions. The call to
setBounds passes four MATLAB scalars of the double
type to the inherited Java Frame method,
setBounds, that takes four
arguments of the
int type. MATLAB converts each 64-bit double data type to
a 32-bit integer prior to making the call. Shown here is the
setBounds method
declaration followed by the MATLAB code that calls the method.
public void setBounds(int x, int y, int width, int height)
frame=java.awt.Frame;
frame.setBounds(200,200,800,400);
frame.setVisible(1);
Passing Built-In Types in an Array
To call a Java method with an argument defined as an array of a built-in type,
you can create and pass a MATLAB matrix with a compatible base type. The
following code defines a polygon by sending four
x and y coordinates to the
Polygon constructor. Two 1-by-4 MATLAB arrays of
double are passed to
java.awt.Polygon, which expects integer arrays in the first two arguments.
Shown here is the Java method declaration followed by MATLAB code that
calls the method, and then verifies the set coordinates.
public Polygon(int xpoints[], int ypoints[], int npoints)
poly = java.awt.Polygon([14 42 98 124], [55 12 -2 62], 4);