Generate Code for Functions with Multiple Signatures - MATLAB & Simulink (original) (raw)
Main Content
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 code. In this case, the generated code 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. Additionally, you can also generate corresponding C/C++ code for each signature. The generated code works with the multiple signatures provided during code generation.
By using multisignature functionality, you can:
- Generate code 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.
Generate Multisignature MEX Function for a Single Entry-Point Function
To generate a multisignature MEX (polymorphic MEX) function, consider this functionmyAdd
:
function y = myAdd(a,b) %#codegen y = a+b; end
Suppose that you want to generate a MEX function frommyAdd
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}
. You specify the entry-point function followed by a -args
for each signature of the entry-point function.
To generate code for myAdd
function, at the MATLAB command prompt, 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 prompt, 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(int8(5),int8(6))
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
andmyAdd_mex
have the same behavior.
Generate Multisignature MEX Function for Multiple Entry-Point Functions
During code generation, you can also generate one MEX function for multiple entry-point functions containing multiple signatures.
Suppose that you have two entry-point functions myAdd
andmyMul
. The first entry-point function, myAdd
returns the sum of two values:
function y = myAdd(a,b) %#codegen y = a+b; end
The second entry-point function, myMul
returns the multiplication of two values:
function y = myMul(a,b) %#codegen y = a*b; end
You specify the entry-point function followed by a -args
for each signature of the entry-point function. Consider that the functionmyAdd
supports the input types double
andint8
. Specify these arguments as: {1,2}
and{int8(1), int8(2)}
. Similarly, if the functionmyMul
supports the input types double
andint16
, specify these arguments as: {1,2}
and{int16(1), int16(2)}
. Now, you can generate a MEX function from your entry-point functions.
To generate code for myAdd
and myMul
functions, at the MATLAB command prompt, run this codegen
command:
codegen -config:mex myAdd.m -args {1,2} -args {int8(1),int8(2)} myMul.m -args {1,2} -args {int16(1),int16(2)} -o 'myMath' -report
This syntax generates one MEX function myMath
for all the signatures that you specified in thecodegen
command.
You can verify the output values by using the generated MEX functionmyMath
at the command prompt. Make sure that the values you pass tomyMath
match the input properties that you specified before code generation.
myMath("myAdd",int8(5),int8(6))
myMath("myMul",int16(5),int16(6))
Running the MATLAB function myAdd
and myMul
with these input values produces the same output. These test cases verify thatmyAdd
, myMul
, and the generated MEX functionmyMath
have the same behavior.