Contenuto principale

Code Generation for Arrays That Grow Via end+1 Indexing

In MATLAB®, you can use the end function to assign a value to the end+1 element of an array. This assignment grows the array. By default, code generation supports this mechanism of growing arrays and cell arrays in your MATLAB code. When you generate code for a MATLAB function that grows an array or cell array by using end+1 indexing, the code generator designates the array dimension that changes size as variable size. To learn about fixed- and variable-size data in code generation, see Generate Code for Variable-Size Arrays (MATLAB Coder).

Limitations for Arrays and Cell Arrays

To generate code for a MATLAB function that grows an array by using end+1, you must adhere to these restrictions:

  • Do not disable variable-size support. See Enable variable-sizing (MATLAB Coder) or Support variable-size arrays.

  • You can only use end+1 to grow vectors. For example, code generation fails for this function because X is a matrix:

    function X = foo
    X = [1 2;3 4];
    X(end+1,:) = 5;
    end
  • You can use end+1 to grow row and column vectors. However, if you use end+1 to grow an array that is a variable-length column vector at code generation time and a scalar at run time, the generated MEX function can produce an error or the output of the generated code can differ from the MATLAB output. See Resolve Error: Unable to Produce Column Vector from Scalar (MATLAB Coder).

  • You cannot grow an array by assigning a value to an element after end+1, such as end+2.

  • You can grow an empty 1-by-0 or 0-by-0 array by using end+1. However, growing a 0-by-1 array by using end+1 is not supported.

  • You cannot use end+1 to grow structure fields, object properties, or nested function variables.

  • You cannot define a structure array by using dot notation and then use end+1 to grow an array of these structures. For example, code generation fails for this MATLAB function.

    function growStruct
    s.field1 = 5;
    s.field2 = 2;
    s(end+1) = s;
    end
    To grow a structure array by using end+1, define the fields of the structure by using the struct constructor.
    function growStruct
    s = struct("field1",5,"field2",2);
    s(end+1) = s;
    end

Limitations for Cell Arrays

These additional considerations apply when you use {end+1} to grow a cell array:

  • In a MATLAB Function block, you cannot grow a cell array by using {end+1} in a for-loop.

  • If you use {end+1} to grow a cell array that is a variable-size column vector at code generation time and a scalar at run time, the generated MEX function does not produce an error. Instead, it produces a two-element column vector, which means that the output of the generated code differs from the MATLAB output. See Growing Variable-Size Column Cell Array That Is Initialized as Scalar at Run Time.

  • You can only use {end+1} with a variable. You cannot use {end+1} to grow a cell array element that you access via subscripting. For example, code generation fails for this function:

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

    Instead, assign the cell array to a variable and call {end+1} on the variable. For example, code generation succeeds for this function:

    function X = growCellExample
    ca = {1 2 3};
    ca{end+1} = 4;
    X = {'a' ca};
    end

  • You cannot use {end+1} when assigning from a function that returns multiple outputs. For example, code generation fails for this function:

    function [A,B] = growDeal(A,B)
    [A{end+1} B{end+1}] = deal(4,5);
    end

  • You cannot assign a value by using {end+1} inside square brackets. For example, code generation fails for this function:

    function [A,B] = growBracket{A,x)
    [A{end+1}] = x;
    end

  • To use {end+1} to grow a cell array in a loop, the cell array must be variable size. Because only homogenous cell arrays can be variable size, the cell array must also be homogenous, which means that all elements of the array have the same properties. See Code Generation for Cell Arrays (MATLAB Coder).

    For example, code generation for this function succeeds because X is homogenous:

    function out = growHomogenous(n)
    X = {1  2};
    for i = 1:n
        X{end+1} = 2+i;
    end
    out = X;
    end

    However, code generation for this function fails because Y is heterogeneous:

    function out = growHeterogeneous(n)
    Y = {1 'a' 2 'b'};
    for i = 1:n
        Y{end+1} = 2+i;
    end
    out = Y;
    end

See Also

(MATLAB Coder) |

Topics