MATLAB Examples

Generate Code Using Embedded Coder®

This example shows how to select a target for a Simulink® model, configure options, generate C code for embedded systems, and view generated files.

The model represents an 8-bit counter that feeds a triggered subsystem that is parameterized by constant blocks INC, LIMIT, and RESET. Input and Output represent I/O for the model. The Amplifier subsystem amplifies the input signal by gain factor K, which updates when signal equal_to_count is true.


1. Open the model.


2. Open the Configuration Parameters dialog box from the model editor by clicking Simulation > Model Configuration Parameters.

Alternately, type the following commands at the MATLAB® command prompt.

cs = getActiveConfigSet(model);

3. Select the Code Generation node.

4. In the Target Selection pane, click Browse to select a target.

You can generate code for a particular target environment or purpose. Some built-in targeting options are provided using system target files, which control the code generation process for a target.

5. Select the Embedded Real-Time (ERT) target and click Apply.

The ERT target includes a utility to specify and prioritize code generation settings based on your application objectives.

6. In the Code Generation Advisor pane, click Set Objectives.

You can set and prioritize objectives for the generated code. For example, while code traceability might be a very important criterion for your application, you might not want to prioritize it at the cost of code execution efficiency.

7. In the Set Objectives pane, select Execution efficiency and Traceability. Click OK.

You can select and prioritize a combination of objectives before generating code.

8. In the model window, initiate code generation and the build process for the model by using any of the following options:

  • Click the Build Model button.
  • Press Ctrl+B.
  • Select Code > C/C++ Code > Build Model.
  • Invoke the rtwbuild command from the MATLAB command line.
  • Invoke the slbuild command from the MATLAB command line.

9. View the code generation report that appears.

The report includes rtwdemo_rtwecintro.c, associated utility and header files, and traceability and validation reports.

The figure below contains a portion of rtwdemo_rtwecintro.c

10. Close the model.


Related Topics