Main Content

Generate Code for Growing Arrays and Cell Arrays with end + 1 Indexing

Code generation supports growing either an array or a cell array in your MATLAB® code by using end + 1 indexing. By default, code generation supports variable-size arrays. To verify that this functionality is enabled, use one of these approaches:

  • In a code configuration object, make sure that the property EnableVariableSizing is set to true.

  • In the Code Generation Settings dialog box, make sure that the Enable variable-sizing check box is selected.

Grow Array with (end + 1) Indexing

To grow an array X, you can assign a value to X(end + 1). If you make this assignment in your MATLAB code, the code generator treats the dimension you grow as variable-size.

For example, you can generate code for this code snippet:

...
a = [1 2 3 4 5 6];
a(end + 1) = 7;

b = [1 2];
for i = 3:10
    b(end + 1) = i;
end
...

When you use (end + 1) to grow an array, follow these restrictions:

  • Use only (end + 1). Do not use (end + 2), (end + 3), and so on.

  • Use (end + 1) with vectors only. For example, the following code is not allowed because X is a matrix, not a vector.

    ...
    X = [1 2; 3 4];
    X(end + 1) = 5;
    ...
  • You can grow empty arrays of size 1x0 by using (end + 1). Growing arrays of size 0x1 is not supported. Growing an array of size 0x0 is supported only if you create that array by using [].

Growing Variable-Size Column Array That is Initialized as Scalar at Run Time

In MATLAB execution, if you grow a scalar array by using (end+1) indexing, the array grows along the second dimension and produces a row vector. For example, define the function grow:

function z = grow(n, m)
n(end+1) = m;
z = n;
end

Call grow with example inputs:

grow(2,3)
ans =

     2     3

By contrast, in code generation, suppose that:

  • You specify the array to be of variable-size column type (for example, :Inf x 1) at compile time, and

  • Initialize this array as a scalar at run time.

In such situations, the generated code attempts to grow the scalar along the first dimension and therefore, produces a run-time error. For example, generate MEX code for grow. Specify the input n to be a :Inf x 1 double array. Specify the input m to be a double scalar.

codegen grow -args {coder.typeof(0, [Inf 1], [1 0]), 1}
Code generation successful.

Run the generated MEX with the same inputs as before.

grow_mex(2,3)

Attempted to grow a scalar along the first dimension using end+1 indexing. This behavior differs from MATLAB execution which grows a scalar along the second dimension.

How to Avoid This Error.  To avoid this error and grow the array along the column dimension in both generated code and MATLAB execution, rewrite your MATLAB code in either of these ways:

  • Grow the array using the concatenation operator instead of using (end+1). For example, rewrite the grow function as:

    function z = growCat(n, m)
    n = [n;m];
    z = n;
    end
  • In your function, create a temporary variable by transposing the variable that you want to grow. Then, grow this temporary variable by using (end+1) indexing. Finally, take a second transpose of this temporary variable. For example, rewrite the grow function as:

    function z = growTransposed(n, m)
    temp = n';
    temp(end+1) = m;
    z = temp';
    end

Grow Cell Array with {end + 1} Indexing

To grow a cell array X, you can use X{end + 1}. For example:

...
X = {1 2};
X{end + 1} = 'a';
...

When you use {end + 1} to grow a cell array, follow these restrictions:

  • In a MATLAB Function (Simulink) block, do not use {end + 1} in a for-loop.

  • Use only {end + 1}. Do not use {end + 2}, {end + 3}, and so on.

  • Use {end + 1} with vectors only. For example, the following code is not allowed because X is a matrix, not a vector:

    ...
    X = {1 2; 3 4};
    X{end + 1} = 5;
    
    ...

  • Use {end + 1} only with a variable. In the following code, {end + 1} does not cause {1 2 3} to grow. In this case, the code generator treats {end + 1} as an out-of-bounds index into X{2}.

    ...
    X = {'a' { 1 2 3 }};
    X{2}{end + 1} = 4;
    ...

  • When {end + 1} grows a cell array in a loop, the cell array must be variable-size. Therefore, the cell array must be homogeneous.

    This code is allowed because X is homogeneous.

    ...
    X = {1  2};
    for i=1:n
        X{end + 1} = 3;
    end
    ...

    This code is not allowed because X is heterogeneous.

    ...
    X = {1 'a' 2 'b'};
    for i=1:n
        X{end + 1} = 3;
    end
    ...

  • For a coding pattern that causes a difference in behavior between generated code and MATLAB, see Growing Variable-Size Column Cell Array That Is Initialized as Scalar at Run Time.