Products & Services Solutions Academia Support User Community Company

Learn more about Stateflow   

Examples of Integrating Custom C Code in Nonlibrary Models

Accessing Examples of Custom C Code Integration

Follow these steps to access the example models:

  1. Download the zipped file from this location: www.mathworks.com/company/newsletters/digest/june99/stateflow/integration.zip

  2. Extract the contents of the zipped file and store them in a single folder on your local hard drive.

  3. In the MATLAB Command Window, change the current folder to the one where your new files reside.

Example of Using Custom C Code to Define Global Constants

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

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

  2. In the Stateflow Editor, select Tools > Open Simulation Target.

  3. In the Configuration Parameters dialog box, select the Simulation Target > Custom Code pane.

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

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, variables, and functions that apply to all charts in your model.

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

  2. In the Stateflow Editor, select Tools > Open Simulation Target.

  3. In the Configuration Parameters dialog box, select the Simulation Target > Custom Code pane.

  4. Select the Header file subpane.

    In this subpane, you can enter #define and #include statements.

      Note   When you include a custom header file, you must enclose the file name in double quotes.

    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);
  5. Select the Include directories subpane.

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

      Tip   To direct your makefile to look for header or source files in a subfolder relative to the model folder, use this relative path name:

      .\subfolder_name

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

      Tip   To include a source file that resides in a subfolder relative to the model folder, use this relative path name:

      .\subfolder_name\source_file.c

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.

  


Related Products & Applications

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