coder.Constant - Specification of constant value for code generation - MATLAB (original) (raw)

coder.Constant Class

Namespace: 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 acoder.Type object to specify a given constant value in an entry-point input or global variable.

Creation

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

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

Note

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

Properties

expand all

Value — Actual value of constant

constant

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 incodegen with value v as42, the corresponding input type is inferred as double. Similarly, in the second example, when k is passed in codegen with valuev as 42, the corresponding input type is inferred as uint8.

Example: k = coder.Constant(42);

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

Examples

collapse all

Generate a MEX Function from a MATLAB Function by Using Different Input Types

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 and3.

codegen myAdd -args {1,3} -report

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

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

Generate a MEX function myAdd_mex by specifyingcoder.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 value1.

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

Call myAdd_mex with constant input values2 and 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 value5. To fix the error, assign the constant value3, which is the value you passed during compile time.

Generate a MEX Function from a MATLAB Function by Using a Constant Input

Generate MEX code for a MATLAB function that has a constant input. Use theConstantInputs 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.

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 value3.

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

Call myAdd_mex. Provide the input 3 for the second argument.

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 than3, for example 5.

The MEX function ignores the input value 5. It uses the value3, 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 fora. Do not provide a value for the constant argumentb.

Generate C Code from a MATLAB Function That Has Constant Input

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, clickidentity.c.

The function signature for identity is:

Generate a MEX Function from a MATLAB Function That Uses Constant Global Data

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 thatg is a constant global variable with value5.

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.

Limitations

Version History

Introduced in R2011a