Call Reusable Hardware-Independent External Code for Simulation and Code Generation - MATLAB & Simulink (original) (raw)

Code reuse offers business and technological advantages. From a business perspective, code reuse saves time and resources. From a technological perspective, code reuse promotes consistency and reduces memory requirements. Other considerations include:

Examples of reusable hardware-independent algorithmic code to consider importing into the Simulink® environment for simulation and code generation include:

Workflow

To call reusable external algorithm code for simulation and code generation, iterate through the tasks listed in this table.

Choose an Integration Technique

Several techniques are available for integrating reusable hardware-independent algorithmic code into the Simulink environment. Some techniques integrate external code directly. Other approaches convert the external code to Simulink or Stateflow® modeling elements. The integration technique that you choose depends on:

To choose an integration technique for a reusable algorithm, see the subsection that matches the programming language of your external code.

Integration Techniques for External MATLAB Code

Multiple techniques are available for integrating external MATLAB code into the Simulink environment. Use this table to choose the best integration technique for your application.

To embed external MATLAB code in a MATLAB Function or MATLAB System block or generate C or C++ code from MATLAB code with the MATLAB Coder™, the MATLAB code must use functions and classes supported for C/C++ code generation. For more information, see Functions and Objects Supported for C/C++ Code Generation.

Integration Techniques for External C or C++ Code

Multiple techniques are available for integrating external C or C++ code into the Simulink environment. The following table helps you choose the best integration technique for your application.

Condition or Requirement Action More Information
You want to integrate external C code with generated C++ code or conversely Match the language choice for the generated code by modifying the language of the external code. Modify Programming Language of External Code to Match Generated Code
You want to call a simple external C function from your model.Your function code does not require states.You do not want to customize the integration of the code.You want to easily integrate with Simulink Coverage™, Simulink Test™, and Simulink Design Verifier™. Use the SimulinkC Caller block. Integrate C Code Using C Caller Blocks
You want to call multiple external C or C++ functions from a Simulink block or call functions conditionally. You want to include code to preprocess and postprocess data before and after calling external functions.You want to specify different code for simulation and code generation. Use the C Function block Integrate External C/C++ Code into Simulink Using C Function Blocks
Your external C/C++ algorithm models simple dynamic systems with discrete states. You want to call external code for output, start, initialize conditions, and terminate code.Programming experience is limited. You want ease of use with basic flexibility for controlling what code the code generator produces for rapid prototyping. Use the C Function block. Integrate External C/C++ Code into Simulink Using C Function BlocksStart and Terminate Actions Within a C Function Block
Your external C/C++ algorithm models simple dynamic systems with discrete states. You want to call external code for enabling, disabling and updating state code. Programming experience is limited. You want ease of use with basic flexibility for controlling what code the code generator produces for rapid prototyping. Generate S-function and TLC files by using the S-Function Builder. If necessary, refine the generated code manually to meet application requirements. (If you change the generated code, you lose the changes if you regenerate the S-function and TLC files.) Build S-Functions Automatically Using S-Function Builder
Your external C/C++ algorithm models simple dynamic systems that have continuous states.You do not want to manually write a TLC file.Programming experience is limited. You want ease of use with basic flexibility for controlling what code the code generator produces for rapid prototyping. Generate S-function and TLC files by using the S-Function Builder. If necessary, refine the generated code manually to meet application requirements. (If you change the generated code, you lose the changes if you regenerate the S-function and TLC files.) Build S-Functions Automatically Using S-Function Builder
Your external C/C++ algorithm models dynamic systems that have continuous states.You have C/C++ programming experience. You want to control what code the code generator produces for rapid prototyping. Manually write an S-function and TLC file. C/C++ S-Function Basics S-Functions and Code GenerationC S-Function Examples and C++ S-Function Examples
The application requires more entry-point functions than the code generator typically produces—for example, more than_model__step,model_initialize, and_model__terminate. You want maximum flexibility for controlling what code the code generator produces. Manually write an S-function and TLC file. C/C++ S-Function Basics S-Functions and Code GenerationC S-Function Examples and C++ S-Function Examples
Your C/C++ algorithm includes design logic that is based on state machines and flow charts. Or, a function that you want to integrate must exchange data with a model by using global variables. The function defines the global variables and uses them to write output rather than returning a value or writing output to an argument. Add a Stateflow chart to the model. Call the external code from the chart, using C as the action language. In the chart, write code that calls the external function and reads from and writes to the global variables. To perform calculations with output of the external code, the model must read from the global variable during execution. Integrate External Code with Stateflow Charts
You want to include external C/C++ code in a Stateflow chart for simulation and code generation. Configure the model that contains the chart to apply the external C or C++ code. Custom Code (Stateflow)Call C Library Functions in C Charts (Stateflow)Integrate External Code with Stateflow Charts
You want to embed a call to external C/ C++ code from a model quickly. Performance is not an issue. Call the C/C++ code with the coder.ceval function from within a MATLAB Function block. coder.ceval functionIntegrate C Code by Using the MATLAB Function BlockMATLAB Function block

Modify Programming Language of External Code to Match Generated Code

To integrate external C code with generated C++ code or conversely, modify the language of the external code to match the programming language of the generated code. To match the programing language of the external code with the generated code, use the appropriate approach explained below:

#ifdef __cplusplus
extern "C" {
#endif
int my_c_function_wrapper();
#ifdef __cplusplus
}
#endif
The prototype serves as a function wrapper. If your compiler supports C++ code, the value __cplusplus is defined. The linkage specification extern "C" specifies C linkage without name mangling.

Integration Approaches for External Fortran Code

To integrate external Fortran code:

  1. Write an S-function and corresponding TLC file.
  2. Add the S-function to your model.
  3. If necessary, add support files and control model code generation and builds within the Simulink environment.

For more information, see Integrate Fortran Code into Simulink.

See Also

Topics