Main Content

Code Variable-Length Inputs and Outputs for Java Client

MATLAB® supports functions with both variable number of input arguments (varargin) and variable number of output arguments (varargout).

MATLAB Production Server™ Java® client supports the ability to work with variable-length inputs (varargin) and outputs (varargout). varargin supports one or more of any data type supported by MATLAB. See the MATLAB Function Reference for complete information on varargin and varargout.

For example, consider this MATLAB function:

function varargout = vararginout(double1, char2, varargin)
In this example, the first input is type double (double1) and the second input type is a char (char2). The third input is a variable-length array that can contain zero, or one or more input parameters of valid MATLAB data types.

The corresponding client method signature must include the same number of output arguments as the first input to the Java method.

Therefore, the Java method signature supported by MATLAB Production Server Java client, for the varargout MATLAB function, is as follows:

public Object[] vararginout(int nargout, double in1, String in2, Object... vararg);

In the vararginout method signature, you specify equivalent Java types for in1 and in2.

The variable number of input parameters is specified in Java as Object... vararg.

The variable number of output parameters is specified in Java as return type Object[].

Note the following coding best practices illustrated by this example:

  • Both the MATLAB function signature and the Java method signature using the name vararginout. Both signatures define two inputs and two outputs.

  • MATLAB Java interface supports direct conversion from Java double array to MATLAB double array and from Java string to MATLAB char array. For more information, see Conversion of Java Types to MATLAB Types.