| Contents | Index |
| On this page… |
|---|
The demo model sf_frame_sync_controller is an example of using a vector in a Stateflow chart to find a fixed pattern in a data transmission.

For details of how the chart works, see Detection of Valid Transmission Data with Frame Synchronization.
The state get_payload stores complex data in the vector frame, which is of size 221. The entry action assigns the value of (IQ * phasor) to the first element of frame. The during action assigns the next 220 values of (IQ * phasor) to successive elements of frame until you store 221 elements. (For more information, see Using the temporalCount Operator to Index a Vector.)
In the second outgoing transition of the state look_for_sync, the transition action frame = 0 resets all elements of the vector frame to 0 via scalar expansion. (For more information, see Using Scalar Expansion to Assign Values of a Vector or Matrix.)
The demo model sf_pool is an example of using matrices in a Stateflow chart to simulate the opening shot on a pool table.

The model consists of the following blocks.
| Model Component | Description |
|---|---|
| Init chart | Initializes the position and velocity of the cue ball. |
| Pool chart | Calculates the two-dimensional dynamics of each ball on the pool table. |
| Plot block | Animates the motion of each ball during the opening shot. |
| Vel scope | Displays the velocity of each ball during the opening shot. |
| Clock | Provides the instantaneous simulation time to the Plot block. |
To simulate the opening shot, the Pool chart stores two-dimensional data in matrices.
| To store values for... | The Pool chart uses... |
|---|---|
| The instantaneous position of each ball | The 15-by-2 matrix p |
| The instantaneous velocity of each ball | The 15-by-2 matrix v |
| Friction and interaction forces acting on each ball | The 15-by-2 matrix v_dot |
| Boolean data on whether any two balls are in contact | The 15-by-15 matrix ball_interaction |
The Pool chart calculates the motion of each ball on the pool table using MATLAB functions that perform matrix calculations.

| MATLAB Function | Description |
|---|---|
| frictionForce | Calculates the friction force acting on each ball. |
| getBallInteraction | Returns a matrix of Boolean data on whether any two balls are in contact. |
| hasBallInteractionChanged | Returns 1 if ball interactions have changed and 0 otherwise. |
| initBalls | Initializes the position and velocity of every ball on the pool table. |
| interactionForce | Calculates the interaction force acting on each ball. |
| isAnyBallGoingToStop | Returns 1 if any ball has stopped moving and 0 otherwise. |
| isAnyBallNewlyPocketed | Returns 1 if any ball has been newly pocketed and 0 otherwise. |
| isAnyBallOutOfBounds | Returns true if any ball is out of bounds and false otherwise. |
| nearHole | Returns true if a ball is near a pocket on the pool table and false otherwise. |
| pocketNewBalls | Sets the velocity of a ball to 0 if it has been pocketed. |
| resetBallsPosAndVel | Resets the position and velocity of any ball that is out of bounds. |
| updateStopFlags | Keeps track of which balls have stopped moving. |
To run the demo model, follow these steps:
Type sf_pool at the MATLAB command prompt.
Start simulation.
Click anywhere in the animated pool table to specify the initial position of the cue ball.

Click a different spot to specify the initial velocity of the cue ball.

Watch the balls move across the pool table.

![]() | Best Practices for Vectors and Matrices in Stateflow Charts | Using Variable-Size Data 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 |