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 Variable Sizing Restrictions.

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

When the function selects the working dimension automatically, it bases the selection on the upper bounds for the dimension sizes. In the case of the sum function, sum(X) selects its working dimension automatically, while sum(X, dim) uses dim as the explicit working dimension.

For example, if X is a variable-size matrix with dimensions 1x:3x:5, sum(x) behaves like sum(X,2) in generated code. In MATLAB®, it behaves like sum(X,2) provided size(X,2) is not 1. In MATLAB, when size(X,2) is 1, sum(X) behaves like sum(X,3). Consequently, you get a run-time error if an automatically selected working dimension assumes a length of 1 at run time.

To avoid the issue, specify the intended working dimension explicitly as a constant value.

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 Variable Sizing Restrictions

The following restrictions apply to specific toolbox functions, but only for code generation.

FunctionRestrictions with Variable-Size Data
all
any
bsxfun
  • Dimensions expand only where one input array or the other has a fixed length of 1.

cat
  • Dimension argument must be a constant.

  • An error occurs if variable-size inputs are empty at run time.

conv
cov
cross
  • Variable-size array inputs that become vectors at run time must have the same orientation.

deconv
detrend
diag
diff
  • See Automatic dimension restriction.

  • Length of the working dimension must be greater than the difference order input when the input is variable sized. For example, if the input is a variable-size matrix that is 3-by-5 at run time, diff(x,2,1) works but diff(x,5,1) generates a run-time error.

fft
filter
hist
histc
ifft
ind2sub
  • First input (the size vector input) must be fixed size.

interp1
  • For the Y input and xi input, seeArray-to-vector restriction.

  • Y input can become a column vector dynamically.

  • A run-time error occurs if Y input is not a variable-length vector and becomes a row vector at run time.

ipermute
  • Order input must be fixed size.

issorted
magic
  • Argument must be a constant.

  • Output can be fixed-size matrices only.

max
mean
median
min
mode
mtimes
  • When an input is variable-size, MATLAB determines whether to generate code for a general matrix*matrix multiplication or a scalar*matrix multiplication, based on whether one of the arguments is a fixed-size scalar. If neither argument is a fixed-size scalar, the inner dimensions of the two arguments must agree even if a variable-size matrix input is a scalar at run time.

nchoosek
  • The second input, k, must be a fixed-size scalar.

  • The second input, k, must be a constant for static allocation. If you enable dynamic allocation, the second input can be a variable.

  • You cannot create a variable-size array by passing in a variable, k, unless you enable dynamic allocation.

permute
  • Order input must be fixed-size.

planerot
  • Input must be a fixed-size, two-element column vector. It cannot be a variable-size array that takes on the size 2-by-1 at run time.

poly
polyfit
prod
rand
  • For an upper-bounded variable N, rand(1,N) produces a variable-length vector of 1x:M where M is the upper bound on N.

  • For an upper-bounded variable N, rand([1,N]) may produce a variable-length vector of :1x:M where M is the upper bound on N.

Generated fixed-point code enhancements
  • For an upper-bounded variable N, randn(1,N) produces a variable-length vector of 1x:M where M is the upper bound on N.

  • For an upper-bounded variable N, randn([1,N]) may produce a variable-length vector of :1x:M where M is the upper bound on N.

Generated fixed-point code enhancements
  • For an upper-bounded variable N, randn(1,N) produces a variable-length vector of 1x:M where M is the upper bound on N.

  • For an upper-bounded variable N, randn([1,N]) may produce a variable-length vector of :1x:M where M is the upper bound on N.

reshape
  • If the input is a variable-size array and the output array has at least one fixed-length dimension, do not specify the output dimension sizes in a size vector sz. Instead, specify the output dimension sizes as scalar values, sz1,...,szN. Specify fixed-size dimensions as constants.

  • When the input is a variable-size empty array, the maximum dimension size of the output array (also empty) cannot be larger than that of the input.

roots
shiftdim
  • If you do not supply the second argument, the number of shifts is determined at compilation time by the upper bounds of the dimension sizes. Consequently, at run time the number of shifts is constant.

  • An error occurs if the dimension that is shifted to the first dimension has length 1 at run time. To avoid the error, supply the number of shifts as the second input argument (must be a constant).

  • First input argument must have the same number of dimensions when you supply a positive number of shifts.

std
sub2ind
  • First input (the size vector input) must be fixed size.

sum
trapz
typecast
var

Was this topic helpful?