MATLAB Examples

Generate Code and Simulate Models in a Simulink Project

This example shows how to use the code generation template for a new Simulink project. The code generation project template for Simulink Project includes multiple models. The project template also provides utilities (.m scripts) that help you generate controller code and run simulations of the harness model.

To see the Simulink Project API functions for each operation in this example, view the example code.

Contents

Create a Simulink Project

Create a new Simulink Project from the code generation project template.

To create this project from the Simulink start page, in the Command Window, type:

simulink

Select the Code Generation template from the start page, and create the exampleCodeGen project.

Initializing: Project Path
Setting up working folders
Identifying shadowed project files

Generate Code

Generate controller code for the feedback_control.slx model.

To generate controller code, select the Project Shortcuts tab and select the Generate Controller Code shortcut.

This shortcut runs the generate_controller_code.m script in the utilities folder of the project. The script builds the feedback_control.slx model in the controller folder of the project.

Simulate Models

Simulate the top-level harness feedback_harness.slx model.

To open the harness model for simulation, select the Project Shortcuts tab and select the Feedback Harness shortcut. This shortcut opens the feedback_harness.slx model in the harnesses folder of the project.

To simulate the model, click Run.

Observe Simulation Output

Open the Scope block in the model and observe the simulation output.

To open the scope and observe simulation, in the Simulink window, double-click the Scope block.

Use the Dependency Analysis View

To view file dependencies and to open or run files, use the Dependency Analysis view of the project.

From the Dependency Analysis view, you can:

  • Observe file dependencies.
  • Use the context menu for each file to open the file, to run .m script files, and run (simulate) model files.

Closing Project Models
Clearing: Project Path
Closing working folders

More Information

Related Examples