Main Content

Integrate External Code with Stateflow Charts

If your external C or C++ code includes design logic that is based on state machines and flow charts or a function that must exchange data with a model by using global variables, integrate the external code using Stateflow chart. By sharing data and functions between your external code and the Stateflow®chart, you can augment the capabilities of Stateflow and use your preexisting code.

Integrate External Code with Library Charts

To integrate external code with Stateflow library charts for code generation, for each library model that contributes a chart to your main model, complete these steps. Then, generate code.

  1. In the Stateflow Editor, open the Model Configuration Parameters dialog box. Select the parameter Use local custom code settings (do not inherit from main model).

    The library model retains its own custom code settings during code generation.

  2. Specify your custom code in the subpanes.

    Follow the guidelines in Specify Relative Paths to Custom Code (Stateflow).

    If you specified custom code settings for simulation, you can apply these settings to code generation. To avoid entering the same information twice, select Use the same custom code settings as Simulation Target.

  3. Click OK.

After completing these steps for each library model, generate code.

Integrate External Code with All Charts

To integrate external code with all charts for code generation:

  1. Specify custom code options for code generation for your main model.

    1. In the Model Configuration Parameters dialog box, select Code Generation > Custom Code.

    2. In the custom code text fields, specify your custom code.

      Follow the guidelines in Specify Relative Paths to Custom Code (Stateflow).

      If you specified custom code settings for simulation, you can apply these settings to code generation. To avoid entering the same information twice, select Use the same custom code settings as Simulation Target.

  2. Configure code generation for each library model that contributes a chart to your main model. In the Stateflow Editor, open the Model Configuration Parameters dialog box. Clear the parameter Use local custom code settings (do not inherit from main model). The library charts inherit the custom code settings of your main model. Click OK.

  3. Generate code.

Integrate External C Code with Stateflow Charts for Code Generation

This example shows how to use Stateflow® to integrate external code into a model for code generation.

Open Model

model='StateflowCustomCode';
open_system(model);

The model contains a Stateflow chart that accesses the C code function defined in the custom source file my_function.c and the structure defined in the custom header file my_header.h.

eval('edit my_header.h')
eval('edit my_function.c')

my_function simply multiplies its input by 2. The structure in my_header.h is defined as follows:

typedef struct {
            real_T a;
            int8_T b[10];
}MyStruct;

The header file also defines global variables gMyStructVar and *gMyStructPointerVar of the MyStruct type. The chart calls my_function and accesses the structure member using the dot notation gMyStructVar.a and also with the use of a pointer gMyStructPointerVar ->b[1].

Integrate Code

1. On the Model Configuration Parameters dialog box Simulation Target pane, enter the custom source file and header file. Also enter additional include directories and source files.

In this example, the custom header file my_header.c and source file my_function.c are entered on the Simulation Target pane.

Open Configuration Parameters dialog box

configSet = getActiveConfigSet(model);
openDialog(configSet);

2. On the Model Configuration Parameters dialog box Code Generation > Custom Code pane, enter the same custom source file and header file. Also enter the same additional include directories and source files.

In this example, the custom header file my_header.c and source file my_function.c are entered on the Code Generation > Custom Code pane.

Open the Code Generation > Custom Code pane of the Model Configuration Parameters dialog box.

configSet = getActiveConfigSet(model);
openDialog(configSet);

Generate Code

slbuild('StateflowCustomCode')
### Starting build procedure for: StateflowCustomCode
### Successful completion of build procedure for: StateflowCustomCode

Build Summary

Top model targets:

Model                Build Reason                                         Status                        Build Duration
======================================================================================================================
StateflowCustomCode  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 18.303s 

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 18.87s

See Also

Insert External C++ Code into Stateflow Charts for Code Generation

This example shows how to use Stateflow® to integrate external C++ code into a model.

Open Model

model='CppStateflowExternalCode';
open_system(model);

To setup a C++ compiler, on the command line, enter mex -setup.

Integrate C++ Code

1. Create C-style wrapper functions or access macros for each method called from the C action language.

%Open example wrapper functions.
currentFolder = pwd;
filename=fullfile(currentFolder, 'custom_cpp_src', 'adder_cpp.cpp');
edit(filename);
clear filename;

filename=fullfile(currentFolder, 'custom_cpp_src', 'adder_cpp.h');
edit(filename);
clear filename;

2. On the Configuration Parameters dialog box Code Generation > Custom Code pane, enter the S-Function source files and include directories.

%Open Configuration Parameters dialog box
configSet = getActiveConfigSet(model);
openDialog(configSet);

3. On the Configuration Parameters dialog box Simulation Target > Custom Code pane, enter the S-Function source files and include directories.

%Open Configuration Parameters dialog box
configSet = getActiveConfigSet(model);
openDialog(configSet);

Generate C++ Code

On the Configuration Parameters dialog box Code Generation pane, set Language to C++. To generate code, select Build.

%Open Configuration Parameters dialog box
configSet = getActiveConfigSet(model);
openDialog(configSet);
clear model;

Close Model

close_system('CppStateflowExternalCode',0);

See Also

Related Topics