Package: coder
Declare variable-size array
coder.varsize(
'var1'
, 'var2'
,
...)
coder.varsize('var1'
, 'var2'
,
..., ubound
)
coder.varsize('var1'
, 'var2'
,
..., ubound
, dims
)
coder.varsize('var1'
, 'var2'
,
..., [], dims
)
coder.varsize(
declares one or more variables as variable-size data,
allowing subsequent assignments to extend their size. Each 'var1'
, 'var2'
,
...)'varn'
is
the name of a variable or structure field enclosed in quotes. If the
structure field belongs to an array of structures, use colon (:
)
as the index expression to make the field variable-size for all
elements of the array. For example, the expression coder.varsize('data(:).A')
declares
that the field A
inside each element of data
is
variable sized.
coder.varsize(
declares one or
more variables as variable-size data with an explicit upper bound
specified in 'var1'
, 'var2'
,
..., ubound
)ubound
. The argument ubound
must
be a constant, integer-valued vector of upper bound sizes for every
dimension of each 'varn'
.
If you specify more than one 'varn'
,
each variable must have the same number of dimensions.
coder.varsize(
declares
one or more variables as variable size with an explicit upper bound
and a mix of fixed and varying dimensions specified in 'var1'
, 'var2'
,
..., ubound
, dims
)dims
.
The argument dims
is a logical vector,
or double vector containing only zeros and ones. Dimensions that correspond
to zeros or false
in dims
have
fixed size; dimensions that correspond to ones or true
vary
in size. If you specify more than one variable, each fixed dimension
must have the same value across all 'varn'
.
coder.varsize(
declares
one or more variables as variable size with a mix of fixed and varying
dimensions. The empty vector 'var1'
, 'var2'
,
..., [], dims
)[]
means that you
do not specify an explicit upper bound.
When you do not specify ubound
,
the upper bound is computed for each 'varn'
in
generated code.
When you do not specify dims
,
dimensions are assumed to be variable except the singleton ones. A
singleton dimension is a dimension for which size
(A,dim)
=
1.
You must add the coder.varsize
declaration
before each 'varn'
is
used (read). You can add the declaration before the first assignment
to each 'varn'
.
However, for a cell array element, the coder.varsize
declaration
must follow the first assignment to the element. For example:
... x = cell(3, 3); x{1} = [1 2]; coder.varsize('x{1}'); ...
You cannot use coder.varsize
outside the MATLAB® code
intended for code generation. For example, the following code does
not declare the variable, var
, as variable-size
data:
coder.varsize('var',10); codegen -config:lib MyFile -args var
Instead, include the coder.varsize
statement
inside MyFile
to declare var
as
variable-size data.
If you use the cell function to create a cell
array,
you cannot use coder.varsize
with that cell array.
If you use coder.varsize
with
a cell array element, the coder.varsize
declaration
must follow the first assignment to the element. For example:
... x = cell(3, 3); x{1} = [1 2]; coder.varsize('x{1}'); ...
You cannot use coder.varsize
with
global variables.
You cannot use coder.varsize
with MATLAB class
properties.
You cannot use coder.varsize
with string scalars.
coder.varsize
fixes the size of a singleton dimension
unless the dims
argument explicitly specifies that the
singleton dimension has a variable size.
For example, the following code specifies that v
has size
1-by-:10. The first dimension (the singleton dimension) has a fixed size. The
second dimension has a variable
size.
coder.varsize('v', [1 10])
v
has size
:1-by-:10. Both dimensions have a variable
size.coder.varsize('v',[1,10],[1,1])
For a MATLAB Function block, singleton dimensions of input or output signals cannot have a variable size.
If you use input variables (or 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 use coder.varsize
on
the array again, unless you also want to specify an upper bound for
its size.
Using coder.varsize
on an array
without explicit upper bounds causes dynamic memory allocation of
the array. This dynamic memory allocation can reduce the speed of
generated code. To avoid dynamic memory allocation, use the syntax coder.varsize(
to specify an upper
bound for the array size (if you know it in advance).'var1'
, 'var2'
,
..., ubound
)
A cell array can be variable size only if it is homogeneous.
When you use coder.varsize
with a cell array,
the code generator tries to make the cell array homogeneous. It tries
to find a class and size that apply to all
elements of the cell array. For example, if the first element is double
and the second element is 1x2 double, all
elements can be represented as 1x:2 double. If the code generator
cannot find a common class and size, code generation fails. For example,
suppose that the first element of a cell array is char and the second
element is double. The code generator cannot find a class that can
represent both elements.