| Contents | Index |
| On this page… |
|---|
Using MATLAB Functions to Perform Matrix Multiplication and Division |
In Stateflow action language, 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 Defining 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 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 Add > Data and the scope 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 Add > Data and the scope Output to Simulink.
In the Data properties dialog box, enter y in the Name field.
Enter 10 in the Size field.
Click OK.
For information about the temporalCount operator, see Using Temporal Logic in State Actions and Transitions.
![]() | Rules for Using Vectors and Matrices in Stateflow Charts | Examples of Vectors and Matrices in Stateflow Charts | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |