Generate MATLAB Code for Building Fuzzy Systems
After 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")
To generate MATLAB code for creating this system, on the Design tab, select Export > Generate MATLAB Script for a Design.
In the Generate MATLAB Code for a Design dialog box, in the Design list, the active design is automatically selected. In the list, you can select any of the designs from the app design browser.
To generate code, click OK.
The MATLAB Editor opens a generated script with this 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.