Specify Size of Stateflow Data
In Stateflow® charts, data size determines memory allocation, affects simulation performance, and ensures compatibility between Stateflow charts and connected Simulink® blocks. Incorrect sizing can lead to compilation errors, runtime failures, or unexpected behavior.
In a Stateflow chart in a Simulink model, specify the size of a data object by using the Property Inspector or the Stateflow API.
In the Property Inspector, you can set the Size property, as described in Set Data Properties.
With the Stateflow API, you can set the
Sizeproperty.
Data Scope and Sizing Method Compatibility
You can specify data size by specifying a numeric value, a MATLAB expression, or by opting to inherit the size from Simulink. The method you use to specify size depends on your data scope.
| Scope of Data | Inherit the Size | Use Numeric Values | Use MATLAB Expressions |
|---|---|---|---|
| Local | Only in charts that use MATLAB as the action language | Yes | Yes |
| Constant | No | Yes | Yes |
| Parameter | Yes | Yes | Yes |
| Input | Yes | Yes | Yes |
| Output | Yes | Yes | Yes |
| Data store memory | Yes | No | No |
For more information about data scope, see Specify Scope and Type of Stateflow Data.
Specify Data Size by Using Numeric Values
When you specify data size by entering numeric values, use these guidelines.
| Data Type | Size Specification | Example |
|---|---|---|
| Scalar | Enter 1 or leave the field blank | 1 |
| Column vector (n×1) | Enter the number of rows | 5 |
| Row vector (1×n) | Enter [1 n] | [1 5] |
| Matrix (n×m) | Enter [n m] | [3 4] |
| N-dimensional array | Enter [d₁ d₂ ⋯ dₙ] | [2 3 4] |
In charts that use C as the action language, one-dimensional Stateflow vectors are compatible with Simulink row or column vectors of the same size. For example, a Stateflow input data of size 3 is compatible with either:
A Simulink row vector of size
[1 3]A Simulink column vector of size
[3 1]
Specify Data Size by Using Expressions
You can specify data size using MATLAB expressions that evaluate to valid size specifications. Expressions provide flexibility for charts that need dynamic sizing based on parameters or other data.
Expressions can contain:
Numeric values and constants
Parameters and variables
Arithmetic operations (
+,-,*,/).MATLAB function calls. For example,
size,length,floor.
Expressions must evaluate to positive integer values for each dimension.
Values must be type-compatible, such as integers with integers, scalars with compatible types.
Enumerated values must include the type prefix. For example, Colors.Red,
not Red.
Expression Limitations
You cannot use MATLAB expressions to:
Specify inherited data size. In other words, expressions cannot evaluate to
-1.Specify the size of Stateflow input data that accepts frame-based data from Simulink. For more information, see Sample- and Frame-Based Concepts (DSP System Toolbox).
These examples are valid MATLAB expressions for specifying data size in your chart:
K+3, whereKis a chart-level Stateflow constant or parameter.N/2, whereNis a variable in the MATLAB® base workspace.[P Q], wherePandQare Simulink parameters. Charts that use C as the action language propagate these symbolic dimensions throughout the model. See Propagate Symbolic Dimensions of Stateflow Data.2*Colors.Red, whereRedis an enumerated value of typeColors.size(u), whereuis a chart-level variable. The functionsizeenables you to specify the size of one data object based on the size of another data object. This type of expression is useful in a library chart that you reuse with data of different sizes. In other situations, you can improve the clarity of your chart by avoiding thesizefunction and specifying the size of the data directly.floor((a*b)/c), whereaandcare scalars of typeint16andbis a scalar of typedouble.[fi(2,1,16,2) fi(4,1,16,2)]. This expression specifies a data size of[2 4]by calling the functionfi(Fixed-Point Designer). This function returns signed fixed-point numbers with a word length of 16 and a fraction length of 2.
Inherit Data Size
To configure a Stateflow data object to inherit its size from a Simulink signal or its definition in the chart, specify a size of –1.
This approach is useful when:
The data size is determined by the connected Stateflow signal.
You want to maintain flexibility for reusable charts.
The size may change based on model configuration.
After you simulate or build your model, you can find the inherited size of the data in the Model Explorer, under the CompiledSize column.
Propagate Symbolic Dimensions of Stateflow Data
When you select the model configuration parameter Allow symbolic dimension specification, charts that use C as the action language can propagate symbolic dimensions throughout the model.
Propagate Symbolic Dimensions of Stateflow Data
When you select the model configuration parameter Allow symbolic dimension specification, charts that use C as the action language can propagate the symbolic dimensions of Stateflow data throughout the model. If you have Embedded Coder®, the symbolic dimensions go into the generated code for ERT targets. Specify the size of the symbolic dimensions by using Simulink parameters with one of these storage classes:
DefineorImportedDefinewith a specified header fileCompilerFlagA user-defined custom storage class that defines data as a macro in a specified header file
For more information, see Allow symbolic dimension specification (Simulink) and Implement Symbolic Dimensions for Array Sizes in Generated Code (Embedded Coder).
Stateflow charts that use MATLAB as the action language do not support symbolic dimension propagation. To specify data size by using Simulink parameters, clear the Allow symbolic dimension specification check box.
See Also
fi (Fixed-Point Designer) | size