Main Content

Vectors and Matrices in Stateflow Charts

Vectors and matrices combine scalar data into a single, multidimensional data object. You can modify individual elements or perform arithmetic on entire vectors and matrices. For more information, see Operations for Vectors and Matrices in Stateflow.

Define Vector and Matrix Data

  1. Add a data object to your chart, as described in Add Stateflow Data.

  2. Set the Size property for the data object as the dimensions of the vector or matrix. See Specify Size of Stateflow Data. For example:

    • To specify a 4-by-1 column vector, enter 4.

    • To specify a 1-by-4 row vector, enter [1 4].

    • To specify a 3-by-3 matrix, enter [3 3].

  3. Set the Initial value property for the data object. See Initial value.

    • To specify a value of zero for all elements of the vector or matrix, leave the Initial value empty. If you do not specify an initial value, all elements are initialized to 0.

    • To specify the same value for all elements of the vector or matrix, enter a scalar value. All elements are initialized to the scalar value you specify.

    • To specify a different value for each element of the vector or matrix, enter an array of real values. For example:

      • To initialize a 4-by-1 column vector, you can enter [1; 2; 3; 4].

      • To initialize a 1-by-4 row vector, you can enter [1 2 3 4].

      • To initialize a 3-by-3 matrix, you can enter [1 2 3; 4 5 6; 7 8 9].

  4. Set the name, scope, base type, and other properties for the data object, as described in Set Data Properties.

You can specify the size and initial value of a vector or matrix by using an expression. Expressions can contain a mix of numeric values, constants, parameters, variables, arithmetic operations, and calls to MATLAB® functions. For more information, see Specify Data Properties by Using MATLAB Expressions.

Where You Can Use Vectors and Matrices

You can define vectors and matrices at these levels of the Stateflow® hierarchy:

  • Charts

  • Subcharts

  • States

  • Functions

You can use vectors and matrices to define:

  • Input data

  • Output data

  • Local data

  • Function inputs

  • Function outputs

You can also use vectors and matrices as arguments for:

  • State actions

  • Transition actions

  • MATLAB functions

  • Truth table functions

  • Graphical functions

  • Simulink® functions

  • Change detection operators

Rules for Vectors and Matrices in Stateflow Charts

Use Operands of Equal Dimensions for Element-Wise Operations

If you perform element-wise operations on vectors or matrices with unequal dimensions, the chart generates a size mismatch error when you simulate the model. For more information, see Operations for Vectors and Matrices in Stateflow.

Do Not Define Vectors and Matrices with the ml Base Type

The ml base type supports only scalar data. If you define a vector or matrix with the ml base type, the chart generates an error when you simulate the model. For more information, see ml Data Type.

Do Not Use Complex Numbers to Set the Initial Values of Vectors and Matrices

If you initialize an element of a vector or matrix by using a complex number, the chart generates an error when you simulate the model. You can set the values of vectors and matrices to complex numbers after initialization. For more information, see Complex Data in Stateflow Charts.

Do Not Use Vectors and Matrices in Temporal Logic Operators

Because time is a scalar quantity, you cannot use a vector or matrix as an argument for a temporal logic operator. For more information, see Control Chart Execution by Using Temporal Logic.

Related Topics