Control Constant Inputs in MEX Function Signatures

You can control whether a generated MEX function signature includes constant inputs. If you want to use the same test file to run the original MATLAB® function and the MEX function, then the MEX function signature must contain the constant inputs. You can also control whether the run-time values of the constant inputs must match the compile-time values. Checking that the values match can slow down execution speed.

Control MEX Function Signature Using the MATLAB Coder App

  1. To open the Generate dialog box, on the Generate Code page, click the Generate arrow .

  2. Set Build type to MEX.

  3. Click More Settings.

  4. On the All Settings tab, set Constant Inputs to one of the menu options. See Options for Controlling Constant Inputs in MEX Function Signatures.

Control MEX Function Signature at the Command-Line Interface

  1. Create a code configuration object for MEX code generation.

    mexcfg = coder.config('mex');
    

  2. Set the ConstantInputs parameter to 'CheckValues', 'IgnoreValues', or 'Remove' For example:

    mexcfg.ConstantInputs = 'IgnoreValues';

    For a description of the options, see Options for Controlling Constant Inputs in MEX Function Signatures

Options for Controlling Constant Inputs in MEX Function Signatures

The following table lists the options for the:

  • Constant Inputs setting in a project with Output Type set to MEX.

  • ConstantInputs property in a configuration object for MEX code generation.

Constant Inputs (Project)ConstantInputs (Configuration Object)Description

Check values at run time (default)

'CheckValues'

  • The MEX function signature includes the constant inputs. When you call the function, you must provide the constant inputs.

  • The run-time values of the constant inputs must match the compile-time values. When you call the function, you must provide the value that was used at compile-time.

  • Allows you to use the same test file to run the original MATLAB algorithm and the MEX function.

  • Slows down execution of the MEX function.

  • This setting is the default.

Ignore input value

'IgnoreValues'

  • The MEX function signature includes the constant inputs. When you call the function, you must provide the constant inputs.

  • The run-time values of the constant inputs can differ from the compile-time values.

  • Allows you to use the same test file to run the original MATLAB algorithm and the MEX function.

Remove from MEX signature

'Remove'

The MEX function signature does not include the constant inputs. When you call the function, you do not provide the constant inputs.

Call MEX Function with a Constant Input

This example shows how to call MEX functions that have constant inputs. It shows how to use the ConstantInputs parameter to control whether the MEX function signature includes constant inputs and whether the constant input values must match the compile-time values.

Write a function identity that copies its input to its output.

function y = identity(u) %#codegen
y = u;

Create a code configuration object for MEX code generation.

cfg = coder.config('mex');

Generate a MEX function identity_mex with the constant input 42.

codegen identity -config cfg -args {coder.Constant(42)}

Call identity_mex. You must provide the input 42.

identity_mex(42)
ans =

    42

Configure ConstantInputs so that the MEX function does not check that the input value matches the compile-time value.

cfg.ConstantInputs = 'IgnoreValues';

Generate identity_mex with the new configuration.

codegen identity -config cfg -args {coder.Constant(42)}

Call identity_mex with a constant input value other than 42 .

identity_mex(50)
ans =

    42

The MEX function ignored the input value 50.

Configure ConstantInputs so that the MEX function does not include the constant input.

cfg.ConstantInputs = 'Remove';

Generate identity_mex with the new configuration.

codegen identity -config cfg -args {coder.Constant(42)}

Call identity_mex. Do not provide the input value .

identity_mex()
ans =

    42

Was this topic helpful?