Main Content

External Code Integration Examples

This topic shows various scenarios of external code integration.

Integrate External C++ Code into a Model Using S-Functions

This example shows you how to integrate C++ code into a model by using S-functions.

Open Example Model

Open the example model rtwdemo_cpp.

open_system('rtwdemo_cpp');

Configure C++ Compiler

1. In the MATLAB Command Window, enter this command:

mex -setup

2. When prompted, select a C++ compiler that is installed on your development computer.

Integrate C++ Code

You can integrate C++ code into a model by using S-functions.

1. Prepare the S-function source.

2. Open the Simulink Coder or Embedded Coder app.

3. Configure S-function source files and folders. Open the Model Configuration Parameter dialog box, navigate to the Code Generation > Custom Code pane, and add the S-function source files and include folders.

4. Inline the S-function to eliminate overhead of S-functions in the generated code.

Open sfun_cppcount_cpp.tlc

Configure Model for C++ Code Generation

Set model configuration parameter Language to C++.

Generate and review code.

Generate and reviw the C++ code.

Related Topics

Insert External C and C++ Code Into Stateflow Charts for Code Generation

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

3. To generate code, 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('rtwdemo_sfcustom')
### Starting build procedure for: rtwdemo_sfcustom
### Successful completion of build procedure for: rtwdemo_sfcustom

Build Summary

Top model targets built:

Model             Action                       Rebuild Reason                                    
=================================================================================================
rtwdemo_sfcustom  Code generated and compiled  Code generation information file does not exist.  

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

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

Integrate External C Code Into Generated Code By Using Custom Code Blocks and Model Configuration Parameters

This example shows how to place external code in generated code by using custom code blocks and model configuration parameters.

1. Open the model rtwdemo_slcustcode.

open_system('rtwdemo_slcustcode')

2. Open the Simulink Coder or Embedded Coder app.

3. Open the Model Configuration Parameters dialog box and navigate to the Custom Code pane.

4. Examine the settings for model configuration parameters Source file and Initialize function.

  • Source file specifies a comment and sets the variable GLOBAL_INT2 to -1.

  • Initialize function initializes the variable GLOBAL_INT2 to 1.

5. Close the dialog box.

6. Double-click the Model Source block. Block parameter Top of Model Source specifies that the code generator declare the variable GLOBAL_INT1 and set it to 0 at the top of the generated file rtwdemo_slcustcode.c.

7. Open the triggered subsystem Amplifier. The subsystem includes the System Outputs block. The code generator places code that you specify in that block in the generated code for the nearest parent atomic subsystem. In this case, the code generator places the external code in the generated code for the Amplifier subsystem. The external code:

  • Declares the pointer variable *intPtr and initializes it with the value of variable GLOBAL_INT1.

  • Sets the pointer variable to -1 during execution.

  • Resets the pointer variable to 0 before exiting.

8. Generate code and a code generation report.

9. Examine the code in the generated source file rtwdemo_slcustcode.c. At the top of the file, after the #include statements, you find the following declaration code. The example specifies the first declaration with the Source file configuration parameter and the second declaration with the Model Source block.

int_T GLOBAL_INT2 = -1;

int_T GLOBAL_INT1 = 0;

The Output function for the Amplifier subsystem includes the following code, which shows the external code integrated with generated code that applies the gain. The example specifies the three lines of code for the pointer variable with the System Outputs block in the Amplifier subsystem.

int_T *intPtr = &GLOBAL_INT1;

*intPtr = -1;

rtwdemo_slcustcode_Y.Output = rtwdemo_slcustcode_U.Input << 1;

*intPtr = 0;

The following assignment appears in the model initialize entry-point function. The example specifies this assignment with the Initialize function configuration parameter.

GLOBAL_INT2 = 1;

Integrate External C Code Into Generated Code By Using Custom Code Blocks and Model Configuration Parameters

This example shows how to place external code in generated code by using custom code blocks and model configuration parameters.

1. Open the model rtwdemo_slcustcode.

open_system('rtwdemo_slcustcode')

2. Open the Simulink Coder or Embedded Coder app.

3. Open the Model Configuration Parameters dialog box and navigate to the Custom Code pane.

4. Examine the settings for model configuration parameters Source file and Initialize function.

  • Source file specifies a comment and sets the variable GLOBAL_INT2 to -1.

  • Initialize function initializes the variable GLOBAL_INT2 to 1.

5. Close the dialog box.

6. Double-click the Model Source block. Block parameter Top of Model Source specifies that the code generator declare the variable GLOBAL_INT1 and set it to 0 at the top of the generated file rtwdemo_slcustcode.c.

7. Open the triggered subsystem Amplifier. The subsystem includes the System Outputs block. The code generator places code that you specify in that block in the generated code for the nearest parent atomic subsystem. In this case, the code generator places the external code in the generated code for the Amplifier subsystem. The external code:

  • Declares the pointer variable *intPtr and initializes it with the value of variable GLOBAL_INT1.

  • Sets the pointer variable to -1 during execution.

  • Resets the pointer variable to 0 before exiting.

8. Generate code and a code generation report.

9. Examine the code in the generated source file rtwdemo_slcustcode.c. At the top of the file, after the #include statements, you find the following declaration code. The example specifies the first declaration with the Source file configuration parameter and the second declaration with the Model Source block.

int_T GLOBAL_INT2 = -1;

int_T GLOBAL_INT1 = 0;

The Output function for the Amplifier subsystem includes the following code, which shows the external code integrated with generated code that applies the gain. The example specifies the three lines of code for the pointer variable with the System Outputs block in the Amplifier subsystem.

int_T *intPtr = &GLOBAL_INT1;

*intPtr = -1;

rtwdemo_slcustcode_Y.Output = rtwdemo_slcustcode_U.Input << 1;

*intPtr = 0;

The following assignment appears in the model initialize entry-point function. The example specifies this assignment with the Initialize function configuration parameter.

GLOBAL_INT2 = 1;

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='rtwdemo_sfcpp';
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.
filename=fullfile(matlabroot, 'toolbox', 'rtw', 'rtwdemos', 'custom_cpp_src', 'adder_cpp.cpp');
eval('edit(filename)');
clear filename;

filename=fullfile(matlabroot, 'toolbox', 'rtw', 'rtwdemos', 'custom_cpp_src', 'adder_cpp.h');
eval('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;

See Also

Close Model

rtwdemoclean;
close_system('rtwdemo_sfcpp',0);