| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Stateflow |
| Contents | Index |
| Learn more about Stateflow |
Follow these steps to access the example models:
Download the zipped file from this location: www.mathworks.com/company/newsletters/digest/june99/stateflow/integration.zip
Extract the contents of the zipped file and store them in a single folder on your local hard drive.
In the MATLAB Command Window, change the current folder to the one where your new files reside.
This example describes how to use custom C code to define constants that apply to all charts in your model.
Open the file named example1.mdl in the Simulink model window.

This chart also appears in the Stateflow Editor.

The chart contains two states A and B and one Simulink input named input_data, which you can set to 0 or 1 by flipping the Manual Switch in the model during simulation.
In the Stateflow Editor, select Tools > Open Simulation Target.
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 (example1_sfun.h), you can use TRUE and FALSE in all charts that belong to this model.
This example describes how to use custom C code to define constants, variables, and functions that apply to all charts in your model.
Open the file named example2.mdl in the Simulink model window.

This chart also appears in the Stateflow Editor.

The chart contains two states A and B and three data objects: input_data, local_data, and out_data. The chart calls a custom function named my_function and accesses a custom variable named myglobal.
In the Stateflow Editor, select Tools > Open Simulation Target.
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 example2_hdr.h contains definitions of three constants:
#define TRUE 1 #define FALSE 0 #define MAYBE 2
This header file also contains declarations for the function my_function and the variable myglobal:
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 example2.mdl.
Select the Source files subpane.

The custom source file example2_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 (example2_sfun.h), you can access them in all charts that belong to this model.
![]() | Choosing a Compiler | How to Build a Stateflow Custom Target | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |