main content

specification of constant value for code generation -凯发k8网页登录

package: coder
superclasses:

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

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

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.

version history

introduced in r2011a

see also

| | | (fixed-point designer)

topics

    网站地图