Generate Code Using Simulink® Coder™

This example shows how to select a system target file for a Simulink® model, generate C code for real-time simulation, 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. For example, type the following commands at the MATLAB® command prompt.


2. Open the Configuration Parameters dialog box from the model editor by clicking Simulation > 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 Generic Real-Time (GRT) target and click Apply.

Optionally, in the Code Generation Advisor pane set the Select objective field to Execution efficiency or Debugging. Then click Check model... to identify and systematically change parameters to meet your objectives.

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

7. View the code generation report that appears.

The report includes links to model files such as rtwdemo_rtwintro.c and associated utility and header files.

The figure below contains a portion of rtwdemo_rtwintro.c

8. Close the model.


Was this topic helpful?