Use vectors and matrices when you want to:

Process multidimensional input and output signals

Combine separate scalar data into one signal

For examples, see Find Patterns in Data Transmission by Using Vectors and Calculate Motion by Using 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

^{®}functionsTruth table functions

Graphical functions

Simulink

^{®}functionsChange detection operators

For more information, see Supported Operations for Vectors and Matrices and Rules for Vectors and Matrices in Stateflow Charts.

These rules apply when you use vectors and matrices in Stateflow charts.

Use only operands of equal dimensions for element-wise operations

Do not define vectors and matrices with ml base type

Use only real numbers to set initial values of vectors and matrices

Do not use vectors and matrices with temporal logic operators

In a Stateflow chart, the multiplication and division operators perform element-wise multiplication and division. Use a MATLAB function to perform standard matrix multiplication and division.

For example, suppose that you want to perform standard matrix operations on two square matrices during simulation. Follow these steps:

In your chart, add a MATLAB function with the following signature:

`[y1, y2, y3] = my_matrix_ops(u1, u2)`

Double-click the function box to open the editor.

In the editor, enter the code below.

function [y1, y2, y3] = my_matrix_ops(u1, u2) %#codegen y1 = u1 * u2; % matrix multiplication y2 = u1 \ u2; % matrix division from the right y3 = u1 / u2; % matrix division from the left

This function computes three values:

`y1`

is the product of two input matrices`u1`

and`u2`

.`y2`

is the matrix that solves the equation`u1 * y2 = u2`

.`y3`

is the matrix that solves the equation`y3 * u1 = u2`

.

Set properties for the input and output data.

Open the Model Explorer.

In the

**Model Hierarchy**pane, navigate to the level of the MATLAB function.In the

**Contents**pane, set properties for each data object.

### Note

To initialize a matrix, see Define a Matrix.

When you index a vector, you can use the `temporalCount`

operator to avoid using an extra variable for the index counter. This indexing
method works for vectors that contain real or complex data.

For example, suppose that you want to collect input data in a buffer during simulation. Follow these steps:

Add this state to your Stateflow chart.

The state

`Collect_Data`

stores data in the vector`y`

, which is of size`10`

. The`entry`

action assigns the value of input data`u`

to the first element of`y`

. The`during`

action assigns the next nine values of input data to successive elements of the vector`y`

until you store ten elements.Add the input data

`u`

to the chart.In the Stateflow Editor, select

**Chart**>**Add Inputs & Outputs**>**Data Input From Simulink**.In the Data properties dialog box, enter

`u`

in the**Name**field.Click

**OK**.

Add the output data

`y`

to the chart.In the Stateflow Editor, select

**Chart**>**Add Inputs & Outputs**>**Data Output To Simulink**.In the Data properties dialog box, enter

`y`

in the**Name**field.Enter

`10`

in the**Size**field.Click

**OK**.

### Note

You do not need to set initial values for this output vector. By default, all elements initialize to 0.

For information about the `temporalCount`

operator, see
Control Chart Execution by Using Temporal Logic.