Documentation

Integrate External C and C++ Code Into a Model for Code Generation Using Stateflow

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

Open Model

model='rtwdemo_sfcustom';
open_system(model);

Integrate Code

1. The example includes the custom header file my_header.c and the custom source file my_function.c.

%Open files my_header.h and my_function.c
eval('edit my_header.h')
eval('edit my_function.c')

2. On the Configuration Parameters dialog box Simulation Target > Custom Code 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 > Custom Code pane.

%Open Configuration Parameters dialog box
slCfgPrmDlg(model,'Open');
slCfgPrmDlg(bdroot,'TurnToPage','Simulation Target/Custom Code');

3. If you generate code with Simulink Coder®, on the 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 Configuration Parameters dialog box
slCfgPrmDlg(model,'Open');
slCfgPrmDlg(bdroot,'TurnToPage','Code Generation/Custom Code');

Generate Code

rtwbuild('rtwdemo_sfcustom')
### Starting build procedure for model: rtwdemo_sfcustom
### Successful completion of build procedure for model: rtwdemo_sfcustom

Call C Code from Stateflow

To call custom C code functions from Stateflow, use the same syntax as graphical function calls: result = my_custom_function(in_args);

To call variables of structure type, use the dot notation: result = my_var.my_field;

See Also

Close Model

rtwdemoclean;
close_system('rtwdemo_sfcustom',0);
Was this topic helpful?