Main Content

Generate MATLAB Code for Building Fuzzy Systems

Since R2024a

Once you interactively create a fuzzy system using Fuzzy Logic Designer, you can generate MATLAB® code to programmatically create that system. You can then use or modify the generated code for your applications.

For this example, you generate code for the tipper system created in Build Fuzzy Systems Using Fuzzy Logic Designer. Open the app and import the tipper system.

fuzzyLogicDesigner("tipper.fis")

Fuzzy Logic Designer app showing FIS plot for tipper system.

To generate MATLAB code for creating this system, under Export, click Generate MATLAB script for a Design.

Export menu expanded with cursor over third item titled Generate MATLAB script for a design

In the Generate MATLAB Code for a Design dialog box, in the Design drop-down list, the active design is automatically selected. In the drop-down list, you can select any of the designs from the app design browser.

Generate MATLAB Code for a Design dialog box showing tipper selected in Design drop-down list.

To generate code, click OK.

The MATLAB Editor opens a generated script with the following code.

% Construct FIS
tipper = mamfis(Name="tipper");
% Input 1
tipper = addInput(tipper,[0 10],Name="service");
tipper = addMF(tipper,"service","gaussmf",[1.5 0], ...
    Name="poor",VariableType="input");
tipper = addMF(tipper,"service","gaussmf",[1.5 5], ...
    Name="good",VariableType="input");
tipper = addMF(tipper,"service","gaussmf",[1.5 10], ...
    Name="excellent",VariableType="input");
% Input 2
tipper = addInput(tipper,[0 10],Name="food");
tipper = addMF(tipper,"food","trapmf",[0 0 1 3], ...
    Name="rancid",VariableType="input");
tipper = addMF(tipper,"food","trapmf",[7 9 10 10], ...
    Name="delicious",VariableType="input");
% Output 1
tipper = addOutput(tipper,[0 30],Name="tip");
tipper = addMF(tipper,"tip","trimf",[0 5 10], ...
    Name="cheap",VariableType="output");
tipper = addMF(tipper,"tip","trimf",[10 15 20], ...
    Name="average",VariableType="output");
tipper = addMF(tipper,"tip","trimf",[20 25 30], ...
    Name="generous",VariableType="output");
% Rules
tipper = addRule(tipper,[1 1 1 1 2; ...
    2 0 2 1 1; ...
    3 2 3 1 2]);

This code:

  • Creates the initial FIS object

  • Adds and configures the input variables

  • Adds and configures the output variable

  • Adds the rules

You can save and modify the generated code for your applications.

See Also

Related Topics