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 how to integrate C++ code into a model by using S-functions.

Open Example Model

Open the example model CppSFunctionExternalCode.

open_system('CppSFunctionExternalCode');

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.

  • Open sfun_cppcount_cpp.cpp.

  • Open sfun_cppcount_cpp.h.

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.

Edit 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='StateflowCustomCode';
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('StateflowCustomCode')
### Starting build procedure for: StateflowCustomCode
### Successful completion of build procedure for: StateflowCustomCode

Build Summary

Top model targets built:

Model                Action                        Rebuild Reason                                    
=====================================================================================================
StateflowCustomCode  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 20.098s

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

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

open_system('CustomCode')

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. On the Additional source code tab, examine the settings for model configuration parameters Additional code and Initialize code.

  • Additional code specifies a comment and sets the variable GLOBAL_INT2 to -1.

  • Initialize code 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 CustomCode.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 CustomCode.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 Additional code 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;

CustomCode_Y.Output = CustomCode_U.Input << 1;

*intPtr = 0;

The following assignment appears in the model initialize entry-point function. The example specifies this assignment with the Initialize code 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 CustomCode.

open_system('CustomCode')

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. On the Additional source code tab, examine the settings for model configuration parameters Additional code and Initialize code.

  • Additional code specifies a comment and sets the variable GLOBAL_INT2 to -1.

  • Initialize code 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 CustomCode.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 CustomCode.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 Additional code 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;

CustomCode_Y.Output = CustomCode_U.Input << 1;

*intPtr = 0;

The following assignment appears in the model initialize entry-point function. The example specifies this assignment with the Initialize code 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='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