Skip to Main Content Skip to Search
Product Documentation

Combining State Actions to Eliminate Redundant Code

State Actions You Can Combine

You can combine entry, during, and exit actions that execute the same tasks in a state.

Why Combine State Actions

By combining state actions that execute the same tasks, you eliminate redundant code. For example:

Separate ActionsEquivalent Combined Actions
entry:
  y = 0;
  y++;
during: y++;
entry: y = 0;
entry, during: y++;
en: 
  fcn1();
  fcn2();
du: fcn1();
ex: fcn1();
en, du, ex: fcn1();
en: fcn2();

Combining state actions this way produces the same chart execution behavior (semantics) and generates the same code as the equivalent separate actions.

See Also

How to Combine State Actions

Combine a set of entry, during, and/or exit actions that perform the same task as a comma-separated list in a state. Here is the syntax:

entry, during, exit: task1; task2;...taskN;

You can also use the equivalent abbreviations:

en, du, ex: task1; task2;...taskN;

Valid Combinations

You can use any combination of the three actions. For example, the following combinations are valid:

You can combine actions in any order in the comma-separated list. For example, en, du: gives the same result as du, en:. See Order of Execution of Combined Actions.

Invalid Combinations

You cannot combine two or more actions of the same type. For example, the following combinations are invalid:

If you combine multiple actions of the same type, you receive a warning that the chart executes the action only once.

Order of Execution of Combined Actions

States execute combined actions in the same order as they execute separate actions:

  1. Entry actions first, from top to bottom in the order they appear in the state

  2. During actions second, from top to bottom

  3. Exit actions last, from top to bottom

The order in which you combine actions does not affect state execution behavior. For example:

This state...Executes actions in this order...

  1. en: y = 0;

  2. en: y++;

  3. du: y++;

  1. en: y++;

  2. en: y = 0;

  3. du: y++;

  1. en: y++;

  2. en: y = 0;

  3. du: y++;

  1. en: y++;

  2. en: y = 10;

  3. du: y++;

  4. ex: y = 10;

Rules for Combining State Actions

  


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