Main Content

Specify Upper Bounds for Variable-Size Arrays

Specify upper bounds for an array when:

  • Dynamic memory allocation is disabled.

    If dynamic memory allocation is disabled, you must specify upper bounds for all arrays.

  • You do not want the code generator to use dynamic memory allocation for the array.

    Specify upper bounds that result in an array size (in bytes) that is less than the dynamic memory allocation threshold.

Specify Upper Bounds for Variable-Size Inputs

如果你通过使用生成代码codegen, to specify upper bounds for variable-size inputs, use thecoder.typeofconstruct with the-argsoption. For example:

codegen foo -args {coder.typeof(double(0),[3 100],1)}

This command specifies that the input to functionfoois a matrix of real doubles with two variable dimensions. The upper bound for the first dimension is 3. The upper bound for the second dimension is 100.

如果你通过使用生成代码theMATLAB®Coder™app, seeSpecify Properties of Entry-Point Function Inputs Using the AppandMake Dimensions Variable-Size When They Meet Size Threshold.

Specify Upper Bounds for Local Variables

When using static allocation, the code generator uses a sophisticated analysis to calculate the upper bounds of local data. However, when the analysis fails to detect an upper bound or calculates an upper bound that is not precise enough for your application, you must specify upper bounds explicitly for local variables.

Constrain the Value of Variables That Specify the Dimensions of Variable-Size Arrays

To constrain the value of variables that specify the dimensions of variable-size arrays, use theassertfunction with relational operators. For example:

functiony = dim_need_bound(n)%#codegenassert (n <= 5); L= ones(n,n); M = zeros(n,n); M = [L; M]; y = M;

Thisassertstatement constrains inputnto a maximum size of 5.Lis variable-size with upper bounds of 5 in each dimension.Mis variable-size with an upper bound of 10 in the first dimension and 5 in the second dimension.

指定上Bounds for All Instances of a Local Variable

To specify the upper bounds for all instances of a local variable in a function, use thecoder.varsizefunction. For example:

functionY = example_bounds1(u)%#codegenY = [1 2 3 4 5]; coder.varsize('Y',[1 10]);if(u > 0) Y = [Y Y+u];elseY = [Y Y*u];end

The second argument ofcoder.varsizespecifies the upper bound for each instance of the variable specified in the first argument. In this example, the argument[1 10]indicates that for every instance ofY:

  • The first dimension is fixed at size 1.

  • The second dimension can grow to an upper bound of 10.

See Also

|

Related Topics