askvity

How to Generate MATLAB Code from Simulink Model?

Published in Simulink Code Generation 4 mins read

Generating code from a Simulink model typically involves using Simulink Coder or Embedded Coder to produce C, C++, or HDL code, rather than pure MATLAB code. However, you can generate various artifacts and reports that integrate with MATLAB, or use specific techniques to generate MATLAB code representing model logic. The primary process for generating code from the model, as per the provided reference, focuses on the code generation settings.

Here's how to configure your Simulink model for code generation:

Configuring for Code Generation

The initial step to prepare your model for code generation involves accessing the model's configuration parameters.

  1. Access Model Settings:

    • In the Modeling tab of the Simulink model toolstrip, locate and click Model Settings. This action opens the Configuration Parameters dialog box.
  2. Configure Code Generation Parameters:

    • Navigate to the Code Generation tab within the Configuration Parameters dialog.
    • Under the General settings, you can select various options. A key parameter mentioned in the reference is Generate code only. Selecting this option configures the process to produce code without building an executable.
    • Click Apply to save your configuration changes.

After configuring these settings, you can initiate the code generation process. This process generates source code files (typically C/C++ or HDL), header files, and associated build scripts and reports based on your model's structure and the chosen target configuration (e.g., a specific embedded processor or a generic real-time system).

Understanding the Output

It's crucial to understand that the standard code generation process from a Simulink model generates C, C++, or HDL code suitable for deployment or simulation acceleration. It does not directly output a .m file containing equivalent MATLAB code that represents the model's logic.

  • Generated Code: The output is source code in languages like C or C++.
  • Build Artifacts: Makefiles or project files for compiling the generated code.
  • Code Generation Reports: HTML reports detailing the code generation process, including traceability between model blocks and generated code.

While you don't get a .m file representing the entire model's logic, MATLAB is extensively used with the generated code:

  • Simulation Acceleration: Generated code can speed up model simulations in MATLAB.
  • Software-in-the-Loop (SIL) / Processor-in-the-Loop (PIL) Simulation: Running generated code in a separate process or on hardware, controlled from MATLAB.
  • Deployment: Compiling the generated C/C++ code and deploying it to target hardware, often managed from MATLAB.

Note: If your goal is to generate MATLAB code that describes the model itself (like how it's built) or to represent specific functions within the model using MATLAB code, different approaches are used:

  • Exporting Model to Script: You can save a model as a MATLAB script (.m) file, which contains commands to recreate the model programmatically. This is done via File > Save As and selecting the .m format.
  • MATLAB Function Blocks: Use MATLAB Function blocks within your Simulink model to define parts of your system using MATLAB code. Code generation then translates this MATLAB code into C/C++ along with the rest of the model.

The primary method for generating code from the Simulink model, as described in the reference, leads to C/C++ or HDL code, not a direct .m file representation of the block diagram logic.

Related Articles