| Contents | Index |
| On this page… |
|---|
Example of Using Custom C Code to Define Global Constants Example of Using Custom C Code to Define Global Constants, Variables, and Functions |
This example describes how to use custom C code to define constants that apply to all charts in your model.
Suppose that you have the following model:

The chart contains the following logic:

The chart contains two states A and B, along with a Simulink input named input_data, which you can set to 0 or 1 by toggling the Manual Switch in the model during simulation.
Open the Configuration Parameters dialog box.
In the Configuration Parameters dialog box, select the Simulation Target > Custom Code pane.
Select the Header file subpane.
In this subpane, you can enter #define and #include statements.

In this example, you define two constants named TRUE and FALSE to move between states in your chart, instead of using the values 1 and 0. These custom definitions improve the readability of your chart actions. Note that TRUE and FALSE are not Stateflow data objects.
Because the two custom definitions appear at the top of your generated machine header file ex_custom_code_global_constants_sfun.h, you can use TRUE and FALSE in all charts that belong to this model. For more information about generated files, see Code Files for a Simulation Target.
This example describes how to use custom C code to define constants, variables, and functions that apply to all charts in your model.
Suppose that you have the following model:

The chart contains the following logic:

The chart contains two states A and B, along with three data objects: input_data, local_data, and out_data. The chart accesses a custom variable named myglobal and calls a custom function named my_function.
Open the Configuration Parameters dialog box.
In the Configuration Parameters dialog box, select the Simulation Target > Custom Code pane.
Select the Header file subpane.
In this subpane, you can enter #define and #include statements.

The custom header file ex_custom_code_global_constants_vars_fcns_hdr.h contains the definitions of three constants:
#define TRUE 1 #define FALSE 0 #define MAYBE 2
This header file also contains declarations for the variable myglobal and the function my_function:
extern int myglobal; extern int my_function(int var1, double var2);
Select the Include directories subpane.

The single period (.) indicates that all your custom code files reside in the same folder as ex_custom_code_global_constants_vars_fcns.mdl.
Select the Source files subpane.

The custom source file ex_custom_code_global_constants_vars_fcns_src.c compiles along with the Stateflow generated code into a single S-function MEX file. See S-Function MEX-Files for details.
In this example, you define three constants, a variable, and a function via custom code options. Because the custom definitions appear at the top of your generated machine header file ex_custom_code_global_constants_vars_fcns_sfun.h, you can access them in all charts that belong to this model. For more information about generated files, see Code Files for a Simulation Target.
![]() | Choosing a Compiler | How to Build a Stateflow Custom Target | ![]() |

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 |