This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Vectors and Matrices in Stateflow Charts

When to Use Vectors and Matrices

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.

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

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

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

Best Practices for Vectors and Matrices in Stateflow Charts

Perform Matrix Multiplication and Division Using MATLAB Functions

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:

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

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

  2. Double-click the function box to open the editor.

  3. 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.

  4. Set properties for the input and output data.

    1. Open the Model Explorer.

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

    3. In the Contents pane, set properties for each data object.

    Note

    To initialize a matrix, see Define a Matrix.

Index a Vector Using the temporalCount Operator

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:

  1. 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.

  2. Add the input data u to the chart.

    1. In the Stateflow Editor, select Chart > Add Inputs & Outputs > Data Input From Simulink.

    2. In the Data properties dialog box, enter u in the Name field.

    3. Click OK.

  3. Add the output data y to the chart.

    1. In the Stateflow Editor, select Chart > Add Inputs & Outputs > Data Output To Simulink.

    2. In the Data properties dialog box, enter y in the Name field.

    3. Enter 10 in the Size field.

    4. 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.

Related Topics