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 in the MATLAB® documentation.

Examples

expand 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 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

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

Introduced in R2011a

Was this topic helpful?