Generate C Code by Using Simulink Coder Quick Start Tool

Prepare model rtwdemo_secondOrderSystem for code generation and generate C89/C90 compliant C code by using the Simulink® Coder™ Quick Start tool. Then, inspect the generated code.

Generate Code with Quick Start Tool

The Quick Start tool chooses fundamental code generation settings based on your goals and application. For example, the Quick Start tool configures the model with a fixed-step solver, which is required for code generation.

  1. Open model rtwdemo_secondOrderSystem by entering the model name in the Command Window.

  2. Save a copy of the model to a writeable location on the MATLAB® path.

  3. If the C Code tab is not already open, in the Apps gallery, under Code Generation, click Simulink Coder.

  4. Open the Simulink Coder Quick Start tool. On the C Code tab, click Quick Start.

  5. Advance through the steps of the Quick Start tool. Each step asks questions about the code that you want to generate. For this tutorial, use the default settings. The tool validates your selections against the model and presents the parameter changes required to generate code.

  6. In the Generate Code step, apply the proposed changes and generate code from the model by clicking Next.

  7. Click Finish. In the Simulink Editor, return to the C Code tab. Configure code generation customizations, regenerate code, and check results in the code generation report.

Inspect the Generated Code

The code generator creates folder rtwdemo_secondOrderSystem_ert_rtw in your current working folder and places source code files in that folder. The generated code is in two primary files: rtwdemo_secondOrderSystem.c and rtwdemo_secondOrderSystem.h. The file rtwdemo_secondOrderSystem.c contains the algorithm code, including the ODE solver code. Model data and entry-point functions are accessible to a caller by including rtwdemo_secondOrderSystem.h. The rtwdemo_secondOrderSystem.h file includes the extern declarations for block outputs, continuous states, model output, entry points, and timing data.

In your current folder, the code generator creates an slprj/target/_sharedutils folder.. This folder contains the file rtwtypes.h, which defines standard data types that the generated code uses by default. This sibling folder contains generated files that can or must be shared between multiple models.

The code that you generate from a model includes entry-point functions, which you call from application code, such as an external main program. For a rate-based model, these functions include an initialization function, an execution function, and, optionally, terminate and reset functions. The functions exchange data with your application code through a data interface that you control.

  1. Open the code generation report. In the C Code tab, click Open Report.

  2. Open the Code Interface Report section. Review the entry-point functions that the code generator produces for the model. For the initialize, execution (step), and terminate functions, the code generator uses these names:

    • rtwdemo_secondOrderSystem_initialize

    • rtwdemo_secondOrderSystem_step

    • rtwdemo_secondOrderSystem_terminate

    The functions have a void-void interface, which means that they do not pass arguments. The functions gain access to data through shared data structures. Examples of such data include system-level input and output that the functions exchange with application code.

  3. Review the entry-point functions in the generated code. In the left pane of the code generation report, under Generated Code, click file name rtwdemo_secondOrderSystem.c. Use the Find field to find instances of the string secondOrderSystem_step. Use the arrows to the right of the Find field to step through each instance. Do the same for the header file rtwdemo_secondOrderSystem.h. Then, review code for the initialize and terminate functions.

Next, verify whether model simulation results match generated executable program results.