Declare variable-size data
coder.varsize(
declares
that the variables named varName1,...,varNameN
)varName1,...,varNameN
have a variable size.
The declaration instructs the code generator to allow the variables to change size during
execution of the generated code. With this syntax, you do not specify the upper bounds of
the dimensions of the variables or which dimensions can change size. The code generator
computes the upper bounds. All dimensions, except singleton dimensions, are
allowed to change size.
Use coder.varsize
according to these restrictions and guidelines:
Use coder.varsize
inside a MATLAB® function intended for code generation.
The coder.varsize
declaration must precede the first use of a
variable. For
example:
... x = 1; coder.varsize('x'); disp(size(x)); ...
Use coder.varsize
to declare that an output argument has a
variable size or to address size mismatch errors. Otherwise, to define variable-size
data, use the methods described in Define Variable-Size Data for Code Generation.
Note
For MATLAB Function blocks, to declare variable-size input or output
signals, use the Ports and Data Manager. See Declare Variable-Size Inputs and Outputs (Simulink).
If you provide upper bounds in a coder.varsize
declaration, the
upper bounds must match the upper bounds in the Ports and Data Manager.
For more restrictions and guidelines, see Limitations and Tips.
coder.varsize(
also specifies an upper bound for each dimension of the variables.
All variables must have the same number of dimensions.
All dimensions, except singleton dimensions, are
allowed to change size.varName1,...,varNameN
,ubounds
)
coder.varsize(
also specifies an upper bound for each dimension of the variables and whether each dimension
has a fixed size or a variable size. If a dimension has a fixed size, then the corresponding
varName1,...,varNameN
,ubounds
,dims
)ubound
element specifies the fixed size of the dimension.
All variables have the same fixed-size dimensions and the
same variable-size dimensions.
The coder.varsize
declaration 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:
... x = 7; coder.varsize('x', [1,5]); disp(size(x)); ...
After the coder.varsize
declaration, x
is
still a 1-by-1 array. You cannot assign a value to an element beyond the current size of
x
. For example, this code produces a run-time error because the index
3 exceeds the dimensions of
x
.
... x = 7; coder.varsize('x', [1,5]); x(3) = 1; ...
coder.varsize
is not supported for a function input argument. 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
drops upper bounds for
variable-size dimensions.
Limitations for using coder.varsize
with cell arrays:
A cell array can have a variable size only if it is homogeneous. When you use
coder.varsize
with a heterogeneous cell array, the code
generator tries to make the cell array homogeneous. The code generator tries to find a
class and maximum size that apply to all elements of
the cell array. For example, consider the cell array c = {1, [2
3]}
. Both elements can be represented by a double type whose first dimension
has a fixed size of 1 and whose second dimension has a variable size with an upper
bound of 2. If the code generator cannot find a common class and a maximum size, code
generation fails. For example, consider the cell array c = {'a',[2
3]}
. The code generator cannot find a class that can represent both
elements because the first element is char
and the second element
is double
.
If you use the cell
function to define a fixed-size cell
array, you cannot use coder.varsize
to specify that the cell
array has a variable size. For example, this code causes a code generation error
because x = cell(1,3)
makes x
a
fixed-size,1-by-3 cell
array.
... x = cell(1,3); coder.varsize('x',[1 5]) ...
You can use coder.varsize
with a cell array that you define
by using curly braces. For example:
... x = {1 2 3}; coder.varsize('x',[1 5]) ...
To create a variable-size cell array by using the cell
function, use this code
pattern:
function mycell(n) %#codegen x = cell(1,n); for i = 1:n x{i} = i; end end
See Definition of Variable-Size Cell Array by Using cell.
To specify upper bounds for the cell array, use
coder.varsize
.
function mycell(n) %#codegen x = cell(1,n); for i = 1:n x{i} = i; coder.varsize('x',[1,20]); end end
coder.varsize
is not supported for:
Global variables
MATLAB classes or class properties
String scalars
In a code generation report or a MATLAB Function report, a colon (:) indicates that a
dimension has a variable size. For example, a size of 1x:2
indicates
that the first dimension has a fixed size of one and the second dimension has a variable
size with an upper bound of two.
If you use coder.varsize
to specify that the upper bound of a
dimension is 1, by default, the dimension has a fixed size of 1. To specify that the
dimension can be 0 (empty array) or 1, set the corresponding element of the
dims
argument to true
. For example, this code
specifies that the first dimension of x
has a fixed size of 1 and the
other dimensions have a variable size of
5.
coder.varsize('x',[1,5,5])
In contrast, this code specifies that the first dimension of x
has
an upper bound of 1 and has a variable size (can be 0 or
1).
coder.varsize('x',[1,5,5],[1,1,1])
Note
For a MATLAB Function block, you cannot specify that an input or output signal with size 1 has a variable size.
If you use input variables or the result of a computation using input variables to
specify the size of an array, it is declared as variable-size in the generated code. Do
not re-use coder.varsize
on the array, unless you also want to
specify an upper bound for its size.
If you do not specify upper bounds with a coder.varsize
declaration and the code generator is unable to determine the upper bounds, the generated
code uses dynamic memory allocation. Dynamic memory allocation can reduce the speed of
generated code. To avoid dynamic memory allocation, specify the upper bounds by providing
the ubounds
argument.