Main Content

coder.Constant class

Package: coder
Superclasses: coder.Type

Specification of constant value for code generation

Description

Use a coder.Constant object to define input values that are constant during code generation. Use this object with the codegen -args and -globals options to specify the properties of the input arguments and the global variables, respectively. Do not pass it as an input to a generated MEX function.

You can use a coder.Constant object in place of a coder.Type object to specify a given constant value in an entry-point input or global variable.

Creation

const_type = coder.Constant(v) creates a coder.Constant type from the value v.

const_type = coder.newtype('constant', v) creates a coder.Constant type from the value v.

Note

After you have created a coder.Constant object, you can create a constant global variable g that has the value v by using the codegen command: codegen -globals {'g', coder.Constant(v)}.

Properties

expand all

The actual value of the constant. Also indicates the input argument value v that is used to construct the input argument type.

Here, in the first example, when k is passed in codegen with value v as 42, the corresponding input type is inferred as double. Similarly, in the second example, when k is passed in codegen with value v as 42, the corresponding input type is inferred as uint8.

Example: k = coder.Constant(42);

Example: k = coder.Constant(uint8(42));

Examples

collapse all

Write a MATLAB® function myAdd that returns the sum of two values.

function c = myAdd(a,b) %#codegen
c = a + b;
end

Generate a MEX function myAdd_mex. Specify the input arguments as constant with values 1 and 3.

codegen myAdd -args {1,3} -report

Call myAdd_mex with constant input values other than 1 and 3, for example, 2 and 5.

myAdd_mex(2,5)
ans =

     7

The generated MEX function accepts any constant value of the type that you specified in the input argument within the codegen command.

Generate a MEX function myAdd_mex by specifying coder.Constant object as one of the input arguments. Specify the first input argument as double scalar and the second input argument as a constant with value 1.

codegen myAdd -args {1, coder.Constant(3)} -report

Call myAdd_mex with constant input values 2 and 5.

myAdd_mex(2,5)
Constant function parameter 'b' has a different run-time value than the compile-time value.

Error in myAdd_mex

The MEX function displays an error for the input value 5. To fix the error, assign the constant value 3, which is the value you passed during compile time.

Generate MEX code for a MATLAB function that has a constant input. Use the ConstantInputs configuration 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 MATLAB function myAdd that returns the sum of two values.

function c = myAdd(a,b) %#codegen
c = a + b;
end

Create a configuration object for MEX code generation.

mexcfg = coder.config('mex');

Review the value of the constant input checking configuration parameter.

mexcfg.ConstantInputs
ans =

    'CheckValues'

It has the default value.

Generate a MEX function myAdd_mex. Specify that the first argument is a double scalar and the second argument is a constant with value 3.

codegen myAdd -config mexcfg -args {1, coder.Constant(3)}

Call myAdd_mex. 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)}

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)}

Call myAdd_mex. Provide the value 1 for a. Do not provide a value for the constant argument b.

myAdd_mex(1)
ans =

    4

Generate C code for a function specialized for the case where an input has a constant value.

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

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

Create a code configuration object for C code generation.

cfg = coder.config('lib');

Generate C code for identity with the constant input 42, and then generate a report.

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

In the report, on the C code tab, click identity.c.

The function signature for identity is:

double identity(void)

Specify a constant value for a global variable at compile time.

Write a MATLAB function myFunction that returns the value of the global constant g.

function  y = myFunction() %#codegen
global g;

y = g;

end

Create a configuration object for MEX code generation.

cfg = coder.config('mex');

Define a cell array globals that declares that g is a constant global variable with value 5.

globals = {'g', coder.Constant(5)};

Generate a MEX function for myFunction by using the -globals option to specify the global data.

codegen -config cfg -globals globals myFunction

Run the generated MEX function.

myFunction_mex
ans =

     5

Limitations

  • You cannot use coder.Constant on sparse matrices, or on structures, cell arrays, or classes that contain sparse matrices.

Introduced in R2011a