| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
Variable-size data is data whose size may change at run time. By contrast, fixed-size data is data whose size is known and locked at compile time, and therefore cannot change at run time.
You can define variable-size arrays and matrices as inputs, outputs, and local data in Embedded MATLAB Function blocks. However, the block must be able to determine the upper bounds of variable-size data at compile time.
For more information about working with variable-size data in the Embedded MATLAB subset, see Generating Code for Variable-Size Data in the Embedded MATLAB user guide documentation. For more information about using variable-size data in Simulink, see Working with Variable-Size Signals in the Simulink user guide documentation.
Support for variable-size data is enabled by default for Embedded MATLAB Function blocks. To modify this property for individual blocks:
In the Embedded MATLAB Editor, select Tools > Edit Data/Ports
The Ports and Data Manager dialog box opens.
Select or clear the check box Support variable-size arrays.
In the Embedded MATLAB Editor, select Tools > Edit Data/Ports
The Ports and Data Manager dialog box opens.
Click the Add Data icon:
![]()
Select the Variable size check box.
Set Scope as either Input or Output.
Enter size:
| For: | What to Specify |
|---|---|
| Input | Enter -1 to inherit size from Simulink or
specify the explicit size and upper bound. For example, enter [2 4] to specify a 2-D matrix where the upper bounds are 2 for the first dimension and 4 for the second. |
| Output | Specify the explicit size and upper bound. |
Use the function eml.varsize to declare variable-size data locally, as described in Declaring Variable-Size Data in Embedded MATLAB Code in the Embedded MATLAB user guide documentation.
The following example appears throughout this section to illustrate how Embedded MATLAB Function blocks exchange variable-size data with other Simulink blocks. The model uses a variable-size vector to store the values of a white noise signal. The size of the vector may vary at run time as the signal values get pruned by functions that:
Filter out signal values that are not unique within a specified tolerance of each other
Average every two signal values and output only the resulting means
The model that generates and filters the white noise signal looks like this:

The model contains the following blocks:
| Simulink Block | Description |
|---|---|
| Band-Limited White Noise | Generates a set of normally distributed random values as the source of the white noise signal. |
| Embedded MATLAB Function uniquify | Filters out signal values that are not unique to within a specified tolerance of each other. |
| Embedded MATLAB Function avg | Outputs the average of a specified number of unique signal values. |
| Unique values | Scope that displays the unique signal values output from the uniquify function. |
| Average values | Scope that displays the average signal values output from the avg function. |
The band-limited white noise signal has these properties:

The size of the noise power value defines the size of the matrix that holds the signal values — in this case, a 1-by-9 vector of double values.
This block filters out signal values that are not within a tolerance of 0.2 of each other. Here is the code:
function y = uniquify(u) %#eml y = uniquetol(u,0.2);
The uniquify function calls an external Embedded MATLAB function uniquetol to filter the signal values. uniquify passes the 1-by-9 vector of white noise signal values as the first argument and the tolerance value as the second argument. Here is the code for uniquetol:
function B = uniquetol(A,tol) %#eml
A = sort(A);
eml.varsize('B',[1 100]);
B = A(1);
k = 1;
for i = 2:length(A)
if abs(A(k) - A(i)) > tol
B = [B A(i)];
k = i;
end
enduniquetol returns the filtered values of A in an output vector B so that abs(B(i) - B(j)) > tol for all i and j. Every time Simulink samples the Band-Limited White Noise block, it generates a different set of random values for A. As a result, uniquetol may produce a different number of output signals in B each time it is called. To allow B to accommodate a variable number of elements, uniquetol declares it as variable-size data with an explicit upper bound:
eml.varsize('B',[1 100]);In this statement, eml.varsize declares B as a vector whose first dimension is fixed at 1 and whose second dimension can grow to a maximum size of 100. Accordingly, output y of the uniquify block must also be variable sized so it can pass the values returned from uniquetol to the Unique values scope. Here are the properties of y:

For variable-size outputs, you must specify an explicit size and upper bound.
This block averages signal values filtered by the uniquify block and outputs the resulting means to the Average values scope. Here is the code:
function y = avg(u) %#eml k = numel(u)/2; if k == floor(k) y = nway(u,2); else y = u; end
Both input u and output y of avg are declared as variable-size vectors because the number of elements varies depending on how the uniquify function block filters the signal values. Input u inherits its size from the output of uniquify:

The avg function calls an external Embedded MATLAB function nway to calculate the average of every two signal values. Here is the code for nway:
function B = nway(A,n) %#eml
assert(n>=1 && n<=numel(A));
B = zeros(1,numel(A)/n);
k = 1;
for i = 1 : numel(A)/n
B(i) = mean(A(k + (0:n-1)));
k = k + n;
endSimulating the model produces the following results:
The uniquify block outputs a variable number of signal values each time it executes:

The avg block outputs a variable number of signal values each time it executes — approximately half the number of the unique values:

You cannot declare variable-size fields in buses.
The Embedded MATLAB debugger does not display structure fields that are variable-size arrays.
![]() | Working with Structures and Bus Signals | Using Enumerated Data in Embedded MATLAB Function Blocks | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |