Skip to Main Content Skip to Search
Product Documentation

Optimizing Generated Code

About Optimizing Generated Code

This section shows how to configure optimizations and generate code for a model. It shows the generated code before and after the optimizations are applied.

The example model used, example.mdl, is shown below.

Setting Up the Model

First, create the model from Simulink library blocks, and set up basic Simulink and Simulink Coder parameters as follows:

  1. Create a folder, example_codegen, and make it your working folder:

    !mkdir example_codegen
    cd example_codegen
  2. Create a new model and save it as example.mdl.

  3. Add Sine Wave, Gain, and Out1 blocks to your model and connect them as shown in the preceding diagram. Label the signals as shown.

  4. Open Model Explorer by selecting Model Explorer from the model's View menu.

  5. In the Model Hierarchy pane, click the symbol preceding the model name to reveal its components.

  6. Click Configuration (Active) in the left pane.

  7. Select Solver in the center pane. The Solver pane appears at the right.

  8. In the Solver Options pane:

    1. Select Fixed-step in the Type field.

    2. Select discrete (no continuous states) in the Solver field.

    3. Specify 0.1 in the Fixed-step size field. (Otherwise, the Simulink Coder code generator posts a warning and supplies a default value when you generate code.)

  9. Click Apply.

  10. Click Data Import/Export in the center pane and make sure all check boxes in the right pane are cleared. Click Apply if you made any changes.

  11. Select Code Generation in the center pane. Under Target Selection in the right pane, select the default generic real-time (GRT) target grt.tlc.

  12. Select Generate code only at the bottom of the right pane. This option causes the Simulink Coder software to generate code and a make file, then stop at that point, rather than proceeding to invoke make to compile and link the code. Note that the label on the Build button changes to Generate code.

  13. Click Apply.

  14. Save the model.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS