External Interfaces Previous page   Next Page

Passing String Arguments

To call a Java method that has an argument defined as an object of class java.lang.String, you can pass either a String object that was returned from an earlier Java call or a MATLAB 1-by-n character array. If you pass the character array, MATLAB converts the array to a Java object of java.lang.String for you.

For a programming example, see Example - Reading a URL. This shows a MATLAB character array that holds a URL being passed to the Java URL class constructor. The constructor, shown below, expects a Java String argument.

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.

Passing Strings in an Array

When the method you are calling expects an argument of an array of type String, 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.

You create the input argument by storing both strings in a MATLAB cell array. MATLAB converts this structure to a Java array of String.


Previous page  Passing Built-In Data Types Passing Java Objects Next page

© 1994-2005 The MathWorks, Inc.