User`s manual
5 Calling Java from MATLAB
5-50
In the MATLAB call to this constructor, a character array specifying the URL
is passed. MATLAB converts this array to a Java String object prior to calling
the constructor.
url = java.net.URL(...
'http://www.ncsa.uiuc.edu/demoweb/url-primer.html')
Passing Strings in an Array
When the method you are calling expects an argument of an array of type
S
tring, you can create such an array by packaging the strings together in a
MATLAB cell array. The strings can be of varying lengths since you are storing
them in different cells of the array. As part of the method call, MATLAB
converts the cell array to a Java array of
String objects.
In the following example, the
echoPrompts method of a user-written class
accepts a string array argument that MATLAB converted from its original
format as a cell array of strings. The parameter list in the Java method appears
as follows.
public String[] echoPrompts(String s[])
You create the input argument by storing both strings in a MATLAB cell array.
MATLAB converts this structure to a Java array of
String.
myaccount.echoPrompts({'Username: ','Password: '})
ans =
'Username: '
'Password: '
Passing Java Objects
When calling a method that has an argument belonging to a particular Java
class, you must pass an object that is an instance of that class. In the example
below, the
add method belonging to the java.awt.Menu class requires, as an
argument, an object of the
java.awt.MenuItem class. The method declaration
for this is
public MenuItem add(MenuItem mi)
The example operates on the frame created in the previous example in “Passing
Built-In Data Types” on page 5-48. The second, third, and fourth lines of code
shown here add items to a menu to be attached to the existing window frame.