MATLAB Examples

Use Custom Code to Define Global Constants

This example shows how to use custom C code to define constants that apply to all charts in your model.

The model contains a Stateflow® chart with an input, which you can set to 0 or 1 by toggling the manual switch in the model during simulation.

The chart contains two states A and B. In this example, you define two constants named TRUE and FALSE to guard the transitions between the states in the chart, instead of using the values 1 and 0. These custom definitions improve the readability of your chart actions. TRUE and FALSE are not Stateflow data objects.

To enter the custom code that defines the two constants:

  1. Open the Model Configuration Parameters dialog box.
  2. Select the Simulation Target pane.
  3. In the Header file subpane, enter #define and #include statements. For instance, in this example, you define the global constants with this code:
#define TRUE 1
#define FALSE 0

Because the two custom definitions appear at the top of the generated machine header file sf_custom_code_global_constants_sfun.h, you can use TRUE and FALSE in all charts that belong to this model.