Pass Multiple Inputs or Outputs

The plhs and prhs parameters (see Components of Fortran MEX File) are vectors containing pointers to the left side (output) variables and right-hand side (input) variables. plhs(1) contains a pointer to the first left side argument, plhs(2) contains a pointer to the second left side argument, and so on. Likewise, prhs(1) contains a pointer to the first right-hand side argument, prhs(2) points to the second, and so on.

The example xtimesy.F multiplies an input scalar times an input scalar or matrix. To see the source code, open the file in MATLAB® Editor.

As this example shows, creating MEX file gateways that handle multiple inputs and outputs is straightforward. Keep track of which indices of the vectors prhs and plhs correspond to which input and output arguments of your function. In this example, the input variable x corresponds to prhs(1) and the input variable y to prhs(2).

To build this example, at the command prompt type:

mex xtimesy.F

For an input scalar x and a real 3-by-3 matrix, type:

x = 3;
y = ones(3);
z = xtimesy(x, y)
z =
     3     3     3
     3     3     3
     3     3     3
Was this topic helpful?