Constant Input Checking in MEX Functions
When you specify a constant input argument for generation of a MEX function, by default the generated MEX function signature includes this argument. When you call the MEX function, it checks that the value that you provide for the constant argument is the value specified at code generation time.
To generate a MEX function that does not check constant input values or that does not include constant input arguments, modify the constant input checking configuration parameter:
If you use the MATLAB® Coder™ app:
On the Generate Code page, set Build type to
MEX
.Click More Settings.
On the All Settings tab, set Constant Inputs to one of the values in the table.
If you use
codegen
, in a MEX configuration object, set theConstantInputs
property to one of the values in the table.
Constant Inputs (App) | ConstantInputs (Configuration
Object) | Description |
---|---|---|
|
| This value is the default value. When you call the MEX function, it checks that the value you provide for a constant input argument is the value specified at code generation time. You can call the MEX function and the original MATLAB function with the same arguments. Therefore, you can use the same test file for both functions. Checking the values can add to the execution time of the MEX function. |
|
| When you call the MEX function, it ignores the value that you provide for a constant input argument. It uses the value specified at code generation time. You can use the same test file without the overhead of checking the constant argument values. |
|
| The code generator removes constant input arguments from the MEX function signature. When you call the MEX function, you do not provide a value for a constant input argument. This option is for backward compatibility. |
Control Whether a MEX Function Checks the Value of a Constant Input
This example shows how to use the ConstantInputs
parameter to control whether a MEX function checks the value of a constant input argument.
Write a function myadd
that returns the sum of its inputs.
function c = myadd(a,b) c = a + b; end
Create a configuration object for MEX code generation.
mexcfg = coder.config('mex');
Look at the value of the constant input checking configuration parameter, ConstantInputs
.
mexcfg.ConstantInputs
ans = 'CheckValues'
It has the default value, CheckValues
.
Generate a MEX function myadd_mex
. Specify that the first argument is a double scalar and that the second argument is a constant with value 3
.
codegen myadd -config mexcfg -args {1, coder.Constant(3)}
Code generation successful.
Call myadd_mex
. You must provide the input 3
for the second argument.
myadd_mex(1,3)
ans = 4
Modify ConstantInputs
so that the MEX function does not check that the input value matches the value specified at code generation time.
mexcfg.ConstantInputs = 'IgnoreValues';
Generate myadd_mex
.
codegen myadd -config mexcfg -args {1, coder.Constant(3)}
Code generation successful.
Call myadd_mex
with a constant input value other than 3
, for example, 5
.
myadd_mex(1,5)
ans = 4
The MEX function ignores the input value 5
. It uses the value 3
, which is the value that you specified for the constant argument b
when you generated myadd_mex
.
Modify ConstantInputs
so that the MEX function signature does not include the constant input argument.
mexcfg.ConstantInputs = 'Remove';
Generate myadd_mex
.
codegen myadd -config mexcfg -args {1, coder.Constant(3)}
Code generation successful.
Call myadd_mex
. Provide the value 1
for a
. Do not provide a value for the constant argument b
.
myadd_mex(1)
ans = 4