MATLAB Examples

Generate Code for Fuzzy System Using Simulink Coder

You can generate code for a Fuzzy Logic Controller block using Simulink® Coder™. For more information on generating code, see docid:rtw_doccenter.code-generation.


Generate Code for Fuzzy Inference System

By default, the Fuzzy Logic Controller block uses double-precision data for simulation and code generation. The fuzzyPID model is configured to use double-precision data. For more information on configuring your fuzzy inference system for code generation, see docid:fuzzy.bvkr4k8.

mdl = 'fuzzyPID';

It is good practice to validate the performance of the system in Simulink. Run the simulation. The model saves the output response, u, to the MATLAB® workspace.


To generate code for the model, use the docid:rtw_ref.bttxa6u function. For this example, suppress the Command Window output for the build process.

### Starting build procedure for model: fuzzyPID
### Successful completion of build procedure for model: fuzzyPID

By default, Simulink Coder generates C code for a generic real-time target. To select a different target file and language, in the Configuration Parameters dialog box, modify the System target file and Language parameters, respectively.

The generated code is stored in a new fuzzyPID_grt_rtw folder in your current working folder. The name of this folder depends on the selected target file.

On a Windows® system, by default, an executable file named fuzzyPID.exe is also added to the current working folder. To generate code without compilation, in the Configuration parameters dialog box, select the Generate code only parameter before generating code.

Run the executable.

if ispc
    status = system(mdl);
    disp('The example only runs the executable on Windows system.');
** starting the model ** 
** created fuzzyPID.mat ** 

After the executable completes successfully (status = 0), the software creates a fuzzyPID.mat data file that contains the simulation results.

You can compare the output response from the generated code, rtw_y, with the output from the Simulink simulation, y, using the following code:

load fuzzyPID.mat

The result from the generated code matches the Simulink simulation.

You can also generate code for just the controller subsystem in this model. To do so, specify the subsystem when calling the rtwbuild function.

rtwbuild([mdl '/Fuzzy PID'])
### Starting build procedure for model: Fuzzy0
### Successful completion of build procedure for model: Fuzzy0

You can deploy generated code according to your application needs. For example, you can configure the properties of executable files and create static or dynamic libraries. For more information, see docid:rtw_doccenter.bu48ltq.

Generate Code for Other Data Types

The Fuzzy Logic Controller block also supports single-precision and fixed-point data for simulation and code generation. In both cases, your resulting fuzzy system has decreased accuracy compared to an equivalent double-precision fuzzy system. Use:

  • Single-precision data to reduce the memory footprint of your system.
  • Fixed-point data if your target platform only supports fixed-point arithmetic.

To use one of these data types, set the Data type property of the block, and configure the other components in the model to use the same data type.

The fuzzyPID_single model is configured for single-precision data. Open the model.

mdl2 = 'fuzzyPID_single';

In this model, the Data type parameter of the Fuzzy Logic Controller block is set to single. The Fuzzy Logic Controller block automatically converts input signals to the specified data type. Also, the Simulate using parameter is set to Code Generation. The Simulate using option does not affect the code generation process. Instead, setting this option simulates your fuzzy system using the same code path used by generated code.

Generate code for this model.

### Starting build procedure for model: fuzzyPID_single
### Successful completion of build procedure for model: fuzzyPID_single

Setting the Data type parameter of a Fuzzy Logic Controller block ensures that all the inference steps use the specified data type. However, depending on the configuration of other blocks in the model, some of the generated code can still use double-precision data.