Skip to Main Content Skip to Search
Product Documentation

Examples of Integrating Custom C Code in Nonlibrary Models

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

  2. Open the Configuration Parameters dialog box.

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

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

  2. Open the Configuration Parameters dialog box.

  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 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);
  5. 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.

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

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

  


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