Main Content

Generate One MEX Function That Supports Multiple Signatures

An entry-point function is a top-level MATLAB® function from which you generate code. If your entry-point function has inputs, you must specify the properties of the inputs to generate a MEX function. In this case, the generated MEX function works only with the signature of the entry-point function that you specify during code generation.

If your entry-point function supports multiple signatures, you can generate a single MEX function instead of generating a separate MEX function for each signature. The generated MEX function works with the multiple signatures provided during code generation.

By using multisignature MEX functionality, you can:

  • Generate one MEX function that supports the multiple signatures that you specify in the entry-point function.

  • Reduce the overhead involved in generating and using separate MEX functions for each signature of your entry-point function.

  • Achieve MATLAB function-like behavior in the generated MEX function.

Generating Multisignature MEX Function

To generate a multisignature MEX function, consider this function myAdd:

function y = myAdd(a,b)
%#codegen
y = a+b;
end
Suppose that you want to generate a MEX function from myAdd that works with three different data types: double, int8, and vector of doubles. Specify the three arguments as: {1,2}, {int8(2), int8(3)}, and {1:10, 1:10}.

To generate code for myAdd function, at the MATLAB command line, run this codegen command:

codegen -config:mex myAdd.m -args {1,2} -args {int8(2),int8(3)} -args {1:10,1:10} -report
This syntax generates a single MEX function myAdd_mex for the signatures specified in the codegen command.

At the command line, call the generated MEX function myAdd_mex. Make sure that the values you pass to myAdd_mex match the input properties that you specified in the codegen command.

myAdd_mex(3,4)
ans =

     7
myAdd_mex(int8(5),int8(6))
ans =

  int8

   11
myAdd_mex(1:10,2:11)
ans =

     3     5     7     9    11    13    15    17    19    21

Running the MATLAB function myAdd with these input values produces the same output. These test cases verify that myAdd and myAdd_mex have the same behavior.

Limitations

Multisignature MEX generation does not support:

See Also

|

Related Topics