Declare uninitialized variables in code generation



X = coder.nullcopy(A) copies type, size, and complexity of A to X, but does not copy element values. The function preallocates memory for X without incurring the overhead of initializing memory. In code generation, the coder.nullcopy function declares uninitialized variables. In MATLAB®, coder.nullcopy returns the input such that X is equal to A.

If X is a structure or a class containing variable-sized arrays, then you must assign the size of each array. coder.nullcopy does not copy sizes of arrays or nested arrays from its argument to its result.


Before you use X in a function or a program, ensure that the data in X is completely initialized. Declaring a variable through coder.nullcopy without assigning all the elements of the variable results in nondeterministic program behavior. For more information, see How to Eliminate Redundant Copies by Defining Uninitialized Variables.


collapse all

Declare variable X as a 1-by-5 vector of real doubles without performing an unnecessary initialization:

function X = foo %#codegen

N = 5;
X = coder.nullcopy(zeros(1,N));
for i = 1:N
   if mod(i,2) == 0
      X(i) = i;
      X(i) = 0;

Using coder.nullcopy with zeros lets you specify the size of vector X without initializing each element to zero.

Input Arguments

collapse all

Variable to copy, specified as a scalar, vector, matrix, or multidimensional array.

Example: coder.nullcopy(A);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | class
Complex Number Support: Yes


  • You cannot use coder.nullcopy on sparse matrices.

  • You cannot use coder.nullcopy with classes that support overloaded parentheses or require indexing methods to access their data, such as table.

Introduced in R2011a