Main Content

coder.Constant class

Package: coder
Superclasses: coder.Type

Represent set containing one MATLAB value

Description

Use a coder.Constant object to define values that are constant during code generation. Use only with the codegen -args options. Do not pass as an input to a generated MEX function.

Construction

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

codegen -globals {'g', coder.Constant(v)} creates a constant global variable g with the value v.

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

Input Arguments

v

Constant value used to construct the type.

Properties

Value

The actual value of the constant.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.

Examples

collapse all

This example shows how to generate MEX code for a MATLAB® function that has a constant input. It shows how to 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 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.

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

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

This example shows how to generate C code for a function specialized to the case where an input has a constant value.

Write a 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 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)

This example shows how to specify a constant value for a global variable at compile time.

Write a 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 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