coder.const
Fold expressions into constants in generated code
Description
[
evaluates the multi-output function having handle out1,...,outN] = coder.const(handle,arg1,...,argN)handle. It then
replaces out1,...,outN with the results of the evaluation in the
generated code. To learn about the behavior of coder.const when
handle accepts zero inputs and returns zero or one outputs, see Tips (MATLAB Coder).
Examples
Input Arguments
Output Arguments
Limitations
You cannot use
coder.conston dictionaries, dictionary keys, or dictionary values.
Tips
When possible, the code generator constant-folds expressions automatically. Typically, automatic constant-folding occurs for expressions with scalars only. Use
coder.constwhen the code generator does not constant-fold expressions on its own.When constant-folding computationally intensive function calls, to reduce code generation time, make the function call extrinsic. The extrinsic function call causes evaluation of the function call by MATLAB instead of by the code generator. For example:
function j = fcn(z) zTable = coder.const(0:0.01:100); jTable = coder.const(feval('besselj',3,zTable)); j = interp1(zTable,jTable,z); end
See Use coder.const with Extrinsic Function Calls (MATLAB Coder).
If
coder.constis unable to constant-fold a function call, try to force constant-folding by making the function call extrinsic. The extrinsic function call causes evaluation of the function call by MATLAB instead of by the code generator. For example:function yi = fcn(xi) y = coder.const(feval('rand',1,100)); yi = interp1(y,xi); end
See Use coder.const with Extrinsic Function Calls (MATLAB Coder).
Suppose that you call
coder.conston a function handle with zero inputs and zero or one outputs. For example:In such situations, the code generator does not evaluateout = coder.const(@fcn);
fcn, and setsoutto the function handle@fcnitself. To force the evaluation offcnin this special case, call the function explicitly inside thecoder.constcommand. For example:out = coder.const(fcn());
To specify input values or global variables as constants during code generation, use a
coder.Constantobject.
Extended Capabilities
Version History
Introduced in R2013b