In a C 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 C 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
Using Temporal Logic.

Was this topic helpful?