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.
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
You can alternatively create the model using the DSP System template. For more information, see Configure the Simulink Environment for Signal Processing Models.
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. 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:
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:
In the Modeling tab of the model toolstrip, click Model Settings. The Configuration Parameters dialog opens. Navigate to the Code Generation tab, select the Generate code only parameter, and click Apply.
In the Apps gallery, click Simulink Coder. The C Code tab appears. Click the Generate Code 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).
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:
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:
In the Modeling tab of the model toolstrip, click Model Settings. The Configuration Parameters dialog opens. Navigate to the Code Generation tab, clear the Generate code only parameter, and click Apply.
In the C Code tab of the model toolstrip, click the Build 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
To run the generated code, enter the following command in the MATLAB command prompt:
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:
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:
Note that the coefficients in
For more details on building and running the executable, see Generate C Code for a Model (Simulink Coder).
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 generation
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
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.