Skip to Main Content Skip to Search
Product Documentation

Examples of Vectors and Matrices in Stateflow Charts

Communications Example

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.

Storage of Complex Data in a Vector

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

Scalar Expansion of 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.)

Physics Example

The demo model sf_pool is an example of using matrices in a Stateflow chart to simulate the opening shot on a pool table.

How the Model Works

The model consists of the following blocks.

Model ComponentDescription
Init chartInitializes the position and velocity of the cue ball.
Pool chartCalculates the two-dimensional dynamics of each ball on the pool table.
Plot blockAnimates the motion of each ball during the opening shot.
Vel scopeDisplays the velocity of each ball during the opening shot.
ClockProvides the instantaneous simulation time to the Plot block.

Storage of Two-Dimensional Data in Matrices

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 ballThe 15-by-2 matrix p
The instantaneous velocity of each ballThe 15-by-2 matrix v
Friction and interaction forces acting on each ballThe 15-by-2 matrix v_dot
Boolean data on whether any two balls are in contactThe 15-by-15 matrix ball_interaction

Calculation of Two-Dimensional Dynamics of Each Ball

The Pool chart calculates the motion of each ball on the pool table using MATLAB functions that perform matrix calculations.

MATLAB FunctionDescription
frictionForceCalculates the friction force acting on each ball.
getBallInteractionReturns a matrix of Boolean data on whether any two balls are in contact.
hasBallInteractionChangedReturns 1 if ball interactions have changed and 0 otherwise.
initBallsInitializes the position and velocity of every ball on the pool table.
interactionForceCalculates the interaction force acting on each ball.
isAnyBallGoingToStopReturns 1 if any ball has stopped moving and 0 otherwise.
isAnyBallNewlyPocketedReturns 1 if any ball has been newly pocketed and 0 otherwise.
isAnyBallOutOfBoundsReturns true if any ball is out of bounds and false otherwise.
nearHoleReturns true if a ball is near a pocket on the pool table and false otherwise.
pocketNewBallsSets the velocity of a ball to 0 if it has been pocketed.
resetBallsPosAndVelResets the position and velocity of any ball that is out of bounds.
updateStopFlagsKeeps track of which balls have stopped moving.

Running the Demo Model

To run the demo model, follow these steps:

  1. Type sf_pool at the MATLAB command prompt.

  2. Start simulation.

  3. Click anywhere in the animated pool table to specify the initial position of the cue ball.

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

  5. Watch the balls move across the pool table.

  


Free Stateflow Interactive Kit

Learn how engineers use Stateflow to model state machines in their Simulink models.


Get free kit

Trials Available

Try the latest version of Stateflow.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS