Variable-Sizing Restrictions for Code Generation of Toolbox Functions
Common Restrictions
The following common restrictions apply to multiple toolbox functions, but only for code generation. To determine which of these restrictions apply to specific library functions, see the table in Toolbox Functions with Restrictions for Variable-Size Data.
Variable-length vector restriction
Inputs to the library function must be variable-length vectors or
fixed-size vectors. A variable-length vector is a variable-size array that
has the shape 1x:n
or :nx1
(one
dimension is variable sized and the other is fixed at size 1). Other shapes
are not permitted, even if they are vectors at run time.
Automatic dimension restriction
This restriction applies to functions that take the working dimension (the dimension along which to operate) as input. In MATLAB® and in code generation, if you do not supply the working dimension, the function selects it. In MATLAB, the function selects the first dimension whose size does not equal 1. For code generation, the function selects the first dimension that has a variable size or that has a fixed size that does not equal 1. If the working dimension has a variable size and it becomes 1 at run time, then the working dimension is different from the working dimension in MATLAB. Therefore, when run-time error checks are enabled, an error can occur.
For example, suppose that X
is a variable-size
matrix with dimensions 1x:3x:5
. In the generated
code, sum(X)
behaves like sum(X,2)
.
In MATLAB, sum(X)
behaves like sum(X,2)
unless size(X,2)
is
1. In MATLAB, when size(X,2)
is 1, sum(X)
behaves
like sum(X,3)
.
To avoid this issue, specify the intended working dimension
explicitly as a constant value. For example, sum(X,2)
.
Array-to-vector restriction
The function issues an error when a variable-size array that is not a variable-length vector assumes the shape of a vector at run time. To avoid the issue, specify the input explicitly as a variable-length vector instead of a variable-size array.
Array-to-scalar restriction
The function issues an error if a variable-size array assumes a scalar value at run time. To avoid this issue, specify scalars as fixed size.
Toolbox Functions with Restrictions for Variable-Size Data
The following table list functions that have code generation restrictions for variable-size data. For additional restrictions for these functions, and restrictions for all functions and objects supported for code generation, see Functions and Objects Supported for C/C++ Code Generation.
Function | Restrictions for Variable-Size Data |
---|---|
all |
|
any |
|
cat |
|
conv |
|
cov |
|
cross |
|
deconv |
|
detrend |
|
diag |
|
diff |
|
fft |
|
filter |
|
hist |
|
histc |
|
ifft |
|
ind2sub |
|
interp1 |
|
interpft |
|
ipermute |
|
issorted |
|
magic |
|
max |
|
maxk |
|
mean |
|
median |
|
min |
|
mink |
|
mode |
|
mtimes | Consider the multiplication |
nchoosek |
|
paddata |
|
permute |
|
planerot |
|
poly |
|
polyfit |
|
prod |
|
rand |
|
randi |
|
randn |
|
reshape |
|
resize |
|
roots |
|
shiftdim |
|
sort |
|
std |
|
sub2ind |
|
sum |
|
trapz |
|
trimdata |
|
typecast |
|
var |
|
vecnorm |
|