Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Generate C Code from Simulink Model

  • Products Used
  • Simulink® , DSP System Toolbox™ , Simulink Coder™

Simulink Coder generates standalone C and C++ code from Simulink models for deployment in a wide variety of applications. This example generates C code from the ex_codegen_dsp model and builds an executable from the generated code. You can run the executable inside the MATLAB® environment. In addition, you can package and relocate the code to another development environment that does not have the MATLAB and Simulink products installed.

Open the Model

The ex_codegen_dsp model implements a simple adaptive filter to remove noise from a signal while simultaneously identifying a filter that characterizes the noise frequency content. To open this model, enter the following command in MATLAB command prompt:

open_system('ex_codegen_dsp')

You can alternatively create the model using the DSP System template. For more information, see Configure the Simulink Environment for Signal Processing Models.

Configure Model for Code Generation

Prepare the model for code generation by specifying code generation settings in the Configuration Parameters dialog box. Choose the appropriate solver and code generation target, and check the model configuration for execution efficiency. For more details on each of these steps, see Generate C Code for a Model (Simulink Coder).

Simulate the Model

Simulate the model. The Time Scope shows the input and filtered signal characteristics.

The Array Plot shows the last 32 filter weights for which the LMS filter has effectively adapted and filtered out the noise from the signal.

These coefficients can also be accessed using the following command:

filter_wts(:,:,1201)

Generate Code from the Model

Before you generate code from the model, you must first ensure that you have write permission in your current folder.

To generate code, you must make the following changes:

  1. Open the model Configuration Parameters dialog, navigate to the Code Generation tab, and select the Generate code only check box, and click Apply.

  2. From the model toolbar, click the Build Model icon ().

After the model finishes generating code, the Code Generation Report appears, allowing you to inspect the generated code. Note that the build process creates a new subfolder called ex_codegen_dsp_grt_rtw in your current MATLAB working folder. This subfolder contains all the files created by the code generation process, including those that contain the generated C source code. For more information on viewing the generated code, see Generate C Code for a Model (Simulink Coder).

Build and Run the Generated Code

Set Up the C/C++ Compiler

To build an executable, you must set up a supported C compiler. For a list of compilers supported in the current release, see Supported and Compatible Compilers.

To set up your compiler, run the following command in the MATLAB command prompt:

mex –setup

Build the Generated Code

After your compiler is setup, you can build and run the compiled code. The ex_codegen_dsp model is currently configured to generate code only. To build the generated code, you must first make the following changes:

  1. Open the model Configuration Parameters dialog, navigate to the Code Generation tab, and clear the Generate Code Only checkbox.

  2. Click OK to apply your changes and close the dialog box.

  3. From the model toolbar, click the Build Model icon ().

The code generator builds the executable and generates the Code Generation Report. The code generator places the executable in the working folder. On Windows, the executable is ex_codegen_dsp.exe. On Linux, the executable is ex_codegen_dsp.

Run the Generated Code

To run the generated code, enter the following command in the MATLAB command prompt:

!ex_codegen_dsp

Running the generated code creates a MAT-file that contains the same variables as those generated by simulating the model. The variables in the MAT-file are named with a prefix of rt_. After you run the generated code, you can load the variables from the MAT-file by typing the following command at the MATLAB prompt:

load ex_codegen_dsp.mat

You can now compare the variables from the generated code with the variables from the model simulation. To access the last set of coefficients from the generated code, enter the following in the MATLAB prompt:

rt_filter_wts(:,:,1201)

Note that the coefficients in filter_wts(:,:,1201) and rt_filter_wts(:,:,1201) match.

For more details on building and running the executable, see Build and Run Executable (Simulink Coder).

Relocate Code to Another Development Environment

Once you generate code from your Simulink model, you can relocate the code to another development environment using the pack-and-go utility. Use this utility when the development environment does not have the MATLAB and Simulink products.

The pack-and-go utility uses the tools for customizing the build process after code generaton and a packNGo function to find and package files for building an executable image. The files are packaged in a compressed file that you can relocate and unpack using a standard zip utility.

You can package the code by either using the user interface or by using the command-line interface. The command-line interface provides more control over the details of code packaging. For more information on each of these methods, see Relocate Code to Another Development Environment (Simulink Coder).

For an example on how to package the C code and executable generated from this example, see Relocate Code Generated from a Simulink Model to Another Development Environment.

Related Topics

External Websites

Was this topic helpful?