## 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.

FunctionRestrictions for Variable-Size Data
`all`

• An error occurs if you pass the first argument a variable-size matrix that is 0-by-0 at run time.

`any`

• An error occurs if you pass the first argument a variable-size matrix that is 0-by-0 at run time.

`cat`

• Dimension argument must be a constant.

`conv`

• Input vectors must have the same orientation, either both row vectors or both column vectors.

`cov`

`cross`

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

`deconv`

`detrend`

`diag`

`diff`

• 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`

`ipermute`

• Order input must be fixed size.

`issorted`

`magic`

• Argument must be a constant.

• Output can be fixed-size matrices only.

`max`

`maxk`

`mean`

• An error occurs if you pass as the first argument a variable-size matrix that is 0-by-0 at run time.

`median`

• An error occurs if you pass as the first argument a variable-size matrix that is 0-by-0 at run time.

`min`

`mink`

`mode`

• An error occurs if you pass as the first argument a variable-size matrix that is 0-by-0 at run time.

`mtimes`

Consider the multiplication `A*B`. If the code generator is aware that `A` is scalar and `B` is a matrix, the code generator produces code for scalar-matrix multiplication. However, if the code generator is aware that `A` and `B` are variable-size matrices, it produces code for a general matrix multiplication. At run time, if `A` turns out to be scalar, the generated code does not change its behavior. Therefore, when run-time error checks are enabled, a size mismatch error can occur.

`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`

• An error occurs if you pass as the first argument a variable-size matrix that is 0-by-0 at run time.

`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`.

`randi`

• For an upper-bounded variable `N`, `randi(imax,1,N)` produces a variable-length vector of `1x:M` where `M` is the upper bound on `N`.

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

`randn`

• 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. Therefore, 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.

`sort`

`std`

• An error occurs if you pass a variable-size matrix with 0-by-0 dimensions at run time.

`sub2ind`

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

`sum`

• An error occurs if you pass as the first argument a variable-size matrix that is 0-by-0 at run time.

`trapz`

• An error occurs if you pass as the first argument a variable-size matrix that is 0-by-0 at run time.

`typecast`

`var`

• An error occurs if you pass a variable-size matrix with 0-by-0 dimensions at run time.

`vecnorm`

﻿
##### Support 