coder.varsize
Resolve size mismatch errors and declare upper bounds
Syntax
Description
coder.varsize(
instructs
the code generator to allow the dimensions of variables
varName1,...,varNameN
)varName1,...,varNameN
to change size at run time. If you use this
syntax, dimensions with size 1
, otherwise known as singleton dimensions,
remain fixed size. To instruct the code generator to allow singleton dimensions to vary in
size, you must specify variable-size upper bounds by using the
upperBounds
and variableSize
arguments. For more
information about defining variable-size data in MATLAB® code for code generation, see Define Variable-Size Data for Code Generation.
coder.varsize(
specifies an upper bound for each dimension of the variables
varName1,...,varNameN
,upperBounds
)varName1,...,varNameN
. If you use this syntax, singleton dimensions
remain fixed size.
coder.varsize(
specifies, for each upper bound specified in varName1,...,varNameN
,upperBounds
,variableSize
)upperBounds
, whether that
upper bound is fixed size or variable size. To instruct the code generator to allow a
singleton dimension to vary in size, explicitly specify an variable-size upper bound.
Examples
Input Arguments
Limitations
Code generation does not support using
coder.varsize
with global variables, MATLAB classes, and MATLAB class properties.Code generation does not support using
coder.varsize
with strings. See Resolve Issue: coder.varsize Not Supported for Strings (MATLAB Coder).The
coder.varsize
directive instructs the code generator to allow the size of a variable to change. It does not change the size of the variable. Consider this code snippet:... x = 7; coder.varsize("x", [1 5]); disp(size(x)); ...
After the
coder.varsize
directive,x
is still a 1-by-1 array. You cannot assign a value to an element beyond the current size ofx
. For example, this code produces a run-time error because the index 3 exceeds the dimensions ofx
.... x = 7; coder.varsize("x", [1,5]); x(3) = 1; ...
coder.varsize
is not supported for function input arguments. Instead:If the function is an entry-point function, specify that an input argument has a variable size by using
coder.typeof
at the command line. Alternatively, specify that an entry-point function input argument has a variable size by using the Define Input Types step of the app.If the function is not an entry-point function, use
coder.varsize
in the calling function with the variable that is the input to the called function.
For sparse matrices,
coder.varsize
treats variable-size dimensions as unbounded.To use
coder.varsize
with a cell array, the cell array must be homogeneous. See Cell Array Limitations for Code Generation.
Tips
In many cases, the code generator is able to determine that the size of a variable can change at run time. In such cases, the code generator correctly defines the variable as variable size and you do not need to use the
coder.varsize
directive. You should usecoder.varize
only if you see a size overflow error or if you want to specify upper bounds.To declare variable-size output variables in MATLAB Function blocks, use the Symbols pane and the Property Inspector. If you provide upper bounds in a
coder.varsize
declaration, the upper bounds must match the upper bounds in the Property Inspector. See Declare Variable-Size MATLAB Function Block Variables.The code generator uses a colon (
:
) to indicate a variable-size dimension. For example, if the size of an array is3x:5x:Inf
, the first dimension has a fixed size of one, the second dimensions is variable size with an upper bound of five, and the third dimension is unbounded. See Code Generation Reports (MATLAB Coder).If you do not specify upper bounds in the
coder.varsize
declaration and the code generator is unable to infer upper bounds, the generated code uses dynamic memory allocation. Dynamic memory allocation can reduce the speed of the generated code. In some cases, you can avoid dynamic memory allocation by specifying upper bounds by using theupperBounds
argument.If you use
coder.varsize
to specify that the upper bound of a dimension is 1, the dimension has a fixed size of 1 by default. To specify that the dimension can be 0 (empty array) or 1, set the corresponding element of thevariableSize
vector totrue
. For example, this directive specifies that the first dimension ofx
has a fixed size of 1 and the other dimensions have a variable size of5
(1x:5x:5
).coder.varsize('x',[1 5 5])
In contrast, this code specifies that the first dimension of
x
is variable size with an upper bound of 1 (:1x:5x:5
).coder.varsize("x",[1 5 5],[true true true])
When you use the output of an extrinsic function, the code generator is unable to determine the size of this output at code generation time. Use
coder.varsize
to instruct the code generator to treat the variable you use to store this output as variable size. See Use Variable-Size Output of Extrinsic Function at Run Time.Under certain circumstances, you can force a cell array to be homogeneous by using
coder.varsize
. See Control Whether a Cell Array Is Variable-Size.
Version History
Introduced in R2011a