主要内容

coder.const

Fold expressions into constants in generated code

描述

example

出去= coder.const(expression)evaluatesexpression和replaces出去在生成的代码中进行评估的结果。

example

[OUT1,...,OUTN] = coder.const(处理,arg1,...,argN)评估具有句柄的多输出功能处理。然后替换OUT1,...,OUTNwith the results of the evaluation in the generated code.

例子

全部收缩

此示例显示了如何使用生成代码中指定常数coder.const

Write a functionaddshift这是输入的Shift并将其添加到向量的元素中。矢量由前10个自然数的正方形组成。addshift生成此向量。

功能y = addshift(shift)%#codegeny = (1:10).^2+Shift;

Generate code foraddshift使用codegen命令。打开代码生成报告。

codegen-config:lib-launchreportaddshift-args0

代码生成器生成用于创建向量的代码。它添加Shiftto each element of the vector during vector creation. The definition ofaddshiftin generated code looks as follows:

void AddShift(double Shift, double y[10]) { int k; for (k = 0; k < 10; k++) { y[k] = (double)((1 + k) * (1 + k)) + Shift; } }

替换表达式(1:10).^2withcoder.const((1:10)。^2), and then generate code foraddshiftagain using thecodegen命令。打开代码生成报告。

codegen-config:lib-launchreportaddshift-args0

The code generator creates the vector containing the squares of the first 10 natural numbers. In the generated code, it addsShift到该向量的每个元素。定义addshiftin generated code looks as follows:

void addshift(双移,double y [10]){int i;静态const签名char iv [10] = {1、4、9、16、25、36、49、64、81、100};for(i = 0; i <10; i ++){y [i] =(double)iv [i]+shift;}}}

此示例显示了如何将用户写入的函数折叠成生成代码中的常数。

Write a functionGetine这是输入的指数并返回由指数从罪恶表中的查找表中。功能Getine使用另一个功能创建查找表gettable

功能y = getine(索引)%#codegenassert(isa(index,'int32');persistenttbl;如果isempty(tbl)tbl = getTable(1024);endy = tbl(index);功能y = gettable(n) y = zeros(1,n);fori = 1:n y(i) = sin((i-1)/(2*pi*n));end

Generate code forGetineusing an argument of typeINT32。打开代码生成报告。

codegen-config:lib-launchreportGetine-argsINT32(0)

生成的代码包含用于创建查找表的说明。

替换语句:

tbl = gettable(1024);

with:

tbl = coder.const(gettable(1024));

Generate code forGetineusing an argument of typeINT32。打开代码生成报告。

生成的代码包含查找表本身。coder.constforces the expressiongettable(1024)to be evaluated during code generation. The generated code does not contain instructions for the evaluation. The generated code contains the result of the evaluation itself.

此示例显示了如何使用多输出函数在生成代码中指定常数coder.const陈述。

Write a functionMultiplyConst这是输入的factor并乘以两个向量的每个元素VEC1VEC2withfactor。功能generatesVEC1VEC2使用另一个功能EvalConsts

功能[y1,y2] =倍增con(因子)%#codegen[vec1,vec2] = evalconsts(pi。y1 = vec1。*因子;y2 = vec2。*因子;功能[f1,f2] = evalconsts(z,n)f1 = z。^(2*n)/fortorial(2*n);f2 = z。^(2*n+1)/阶乘(2*n+1);

Generate code forMultiplyConst使用codegen命令。打开代码生成报告。

codegen-config:lib-launchreportMultiplyConst-args0

The code generator produces code for creating the vectors.

Replace the statement

[vec1,vec2] = evalconsts(pi。

with

[vec1,vec2] = coder.const(@evalconsts,pi。

Generate code forMultiplyConst使用codegen命令。打开代码生成报告。

codegen-config:lib-launchreportMultiplyConst-args0
The code generator does not generate code for creating the vectors. Instead, it calculates the vectors and specifies the calculated vectors in generated code.

此示例显示了如何使用coder.const

Write an XML filemyparams.xmlcontaining the following statements:

   

节省myparams.xmlin the current folder.

Write a MATLAB®功能xml2struct,上面写着an XML file. The function identifies the XML tagparam在另一个标签内参数

识别后param, the function assigns the value of its attributename到结构的字段名称s。该功能还分配了属性的值价值to the value of the field.

功能s = xml2struct(file)s = struct();doc = xmlread(file);els = doc.getElementsbytagname(“参数”);fori = 0:els.getLength-1 it = els.item(i);ps = it.getElementsbytagname('param');forj = 0:ps.getLength-1 param = ps.item(j); paramName = char(param.getAttribute('name');paramValue = char(param.getAttribute('value');paramValue = evalin('根据',paramvalue);s。(paramname)= paramvalue;endend

节省xml2structin the current folder.

编写一个MATLAB函数MyFunc,上面写着the XML filemyparams.xml进入结构s使用该功能xml2struct。Declarexml2struct作为外部使用coder.extrinsic并称其为coder.const陈述。

功能y = MyFunc(u)%#codegenassert(isa(u,'双倍的');coder.extrinsic('xml2struct');s = coder.const(xml2struct('MyParams.xml');y = s.hello + s.world + u;

Generate code forMyFunc使用codegen命令。打开代码生成报告。

codegen-config:dll-launchreportMyFunc-args0

The code generator executes the call toxml2struct在代码生成期间。它取代了结构字段s.hellos.world具有生成代码中的值17和42。

输入参数

全部收缩

MATLAB表达或用户定义的单输出功能。

该表达式必须仅具有编译时常数。该功能必须仅进行恒定参数。例如,以下代码导致代码生成错误,因为x不是编译时常数。

功能y = func(x)y = coder.const(log10(x));

要解决错误,请分配x达到MATLAB代码中的常数。或者,在代码生成期间,您可以使用Coder.constant定义输入类型如下:

codegen-config:libfunc-argsCoder.constant(10)

例子:2*pi,factorial(10)

Handle to built-in or user-written function.

例子:@日志,@sin

Data Types:功能_handle

用句柄的函数的论点处理

The arguments must be compile-time constants. For instance, the following code leads to a code generation error, becausexy不是编译时常数。

功能y = func(x,y)y = coder.const(@nchoosek,x,y);

要解决错误,请分配xy到MATLAB代码中的常数。或者,在代码生成期间,您可以使用Coder.constant定义输入类型如下:

codegen-config:libfunc-args{coder.Constant(10),coder.Constant(2)}

Output Arguments

全部收缩

Value ofexpression。在生成的代码中MATLABCoder™replaces occurrences of出去with the value ofexpression

Outputs of the function with handle处理MATLABCoderevaluates the function and replaces occurrences ofOUT1,...,OUTNwith constants in the generated code.

提示

  • 在可能的情况下,代码生成器恒定折叠会自动表达。通常,仅使用标量的表达式出现自动恒定折叠。利用coder.const当代码生成器不会单独持续折叠时。

  • 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:

    功能j = fcn(z)ztable = coder.const(0:0.01:100);jtable = coder.const(feval(feval)('Besselj',3,zTable)); j = interp1(zTable,jTable,z);end

    See使用coder.const与外部功能调用(MATLAB编码器)

  • 如果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:

    功能yi = fcn(xi) y = coder.const(feval('兰德',1,100)); yi = interp1(y,xi);end

    See使用coder.const与外部功能调用(MATLAB编码器)

See Also

Topics

在R2013B中引入