addRule throws "Do not use a rule keyword as a variable name."
27 views (last 30 days)
Show older comments
Michael
on 4 Feb 2026 at 10:45
Commented: Walter Roberson
on 6 Feb 2026 at 22:51
I get the error on line 44 of my code: fis = addRule(fis,all); I've tried changing the name of the variable name (all) to other names, and the fis. I've removed spaces from my variables or renamed them completely.
% Create a Fuzzy Inference System (FIS) with the name "IFR"
fis = mamfis('Name', 'IFR');
% Add a variable "input" to the FIS
fis = addInput(fis, [0 200], 'Name', 'MAP_mm');
% Add membership functions for variable "input"
fis = addMF(fis, "MAP_mm", "trapmf", [0 0 55 75], 'Name',"Low");
fis = addMF(fis, "MAP_mm", "trapmf", [55 75 100 120], 'Name',"Normal");
fis = addMF(fis, "MAP_mm", "trapmf", [100 120 200 200], 'Name',"High");
% HOU trapezoidal
fis = addInput(fis, [0 200], 'Name', 'HOU_ml-hr');
% HOU Low trapezoidal
fis = addMF(fis, "HOU_ml-hr", "trapmf", [0 0 30 40], 'Name',"Low");
% HOU Normal trapezoidal
fis = addMF(fis, "HOU_ml-hr", "trapmf", [30 40 100 125], 'Name',"Normal");
% HOU High trapezoidal
fis = addMF(fis, "HOU_ml-hr", "trapmf", [100 125 200 200], 'Name',"High");
% Add output variable to the FIS
fis = addOutput(fis, [0 2000], 'Name', 'IFR_ml-hr');
% Add membership functions for output variable
fis = addMF(fis, "IFR_ml-hr", "trapmf", [0 0 60 100], 'Name',"Low");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [0 100 200 400], 'Name',"Maintain");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [200 400 600 800], 'Name',"Moderate");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [600 800 1000 1500], 'Name',"High");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [1000 1500 2000 2000], 'Name',"Very_High");
% Create Rules for the FIS
r1 = "If HOU_ml-hr == Low & MAP_mm == Low => IFR_ml-hr == Very_High";
r2 = "If HOU_ml-hr == Normal & MAP_mm == Low => IFR_ml-hr == High";
r3 = "If HOU_ml-hr == High & MAP_mm == Low => IFR_ml-hr == Moderate";
r4 = "If HOU_ml-hr == Low & MAP_mm == Normal => IFR_ml-hr == Moderate";
r5 = "If HOU_ml-hr == Low & MAP_mm == High => IFR_ml-hr == Low";
r6 = "If HOU_ml-hr == Normal & MAP_mm == Normal => IFR_ml-hr == Maintain";
r7 = "If HOU_ml-hr == Normal & MAP_mm == High => IFR_ml-hr == Low";
r8 = "If HOU_ml-hr == High & MAP_mm == Normal =>IFR_ml-hr == Maintain";
r9 = "If HOU_ml-hr == High & MAP_mm == High => IFR_ml-hr == Low";
all = [r1 r2 r3 r4 r5 r6 r7 r8 r9];
% Add rules and enable rule viewing/debugging
fis = addRule(fis,all);
% Evaluate the FIS for inputs [MAP HOU]
sampleInput = [110 120; 60 25; 30 150; 180 90];
out = evalfis(fis, sampleInput);
% Plot membership functions
subplot(3,1,1)
plotmf(fis, 'input', 1)
subplot(3,1,2)
plotmf(fis, 'input', 2)
subplot(3,1,3)
plotmf(fis, 'output',3)
0 Comments
Accepted Answer
Walter Roberson
on 4 Feb 2026 at 11:29
Rewriting the rules as words works.
% Create a Fuzzy Inference System (FIS) with the name "IFR"
fis = mamfis('Name', 'IFR');
% Add a variable "input" to the FIS
fis = addInput(fis, [0 200], 'Name', 'MAP_mm');
% Add membership functions for variable "input"
fis = addMF(fis, "MAP_mm", "trapmf", [0 0 55 75], 'Name',"Low");
fis = addMF(fis, "MAP_mm", "trapmf", [55 75 100 120], 'Name',"Normal");
fis = addMF(fis, "MAP_mm", "trapmf", [100 120 200 200], 'Name',"High");
% HOU trapezoidal
fis = addInput(fis, [0 200], 'Name', 'HOU_ml-hr');
% HOU Low trapezoidal
fis = addMF(fis, "HOU_ml-hr", "trapmf", [0 0 30 40], 'Name',"Low");
% HOU Normal trapezoidal
fis = addMF(fis, "HOU_ml-hr", "trapmf", [30 40 100 125], 'Name',"Normal");
% HOU High trapezoidal
fis = addMF(fis, "HOU_ml-hr", "trapmf", [100 125 200 200], 'Name',"High");
% Add output variable to the FIS
fis = addOutput(fis, [0 2000], 'Name', 'IFR_ml-hr');
% Add membership functions for output variable
fis = addMF(fis, "IFR_ml-hr", "trapmf", [0 0 60 100], 'Name',"Low");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [0 100 200 400], 'Name',"Maintain");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [200 400 600 800], 'Name',"Moderate");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [600 800 1000 1500], 'Name',"High");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [1000 1500 2000 2000], 'Name',"Very_High");
% Create Rules for the FIS
r1 = "If HOU_ml-hr is Low and MAP_mm is Low then IFR_ml-hr is Very_High";
r2 = "If HOU_ml-hr is Normal and MAP_mm is Low then IFR_ml-hr is High";
r3 = "If HOU_ml-hr is High and MAP_mm is Low then IFR_ml-hr is Moderate";
r4 = "If HOU_ml-hr is Low and MAP_mm is Normal then IFR_ml-hr is Moderate";
r5 = "If HOU_ml-hr is Low and MAP_mm is High then IFR_ml-hr is Low";
r6 = "If HOU_ml-hr is Normal and MAP_mm is Normal then IFR_ml-hr is Maintain";
r7 = "If HOU_ml-hr is Normal and MAP_mm is High then IFR_ml-hr is Low";
r8 = "If HOU_ml-hr is High and MAP_mm is Normal then IFR_ml-hr is Maintain";
r9 = "If HOU_ml-hr is High and MAP_mm is High then IFR_ml-hr is Low";
all = [r1 r2 r3 r4 r5 r6 r7 r8 r9];
% Add rules and enable rule viewing/debugging
fis = addRule(fis,all);
% Evaluate the FIS for inputs [MAP HOU]
sampleInput = [110 120; 60 25; 30 150; 180 90];
out = evalfis(fis, sampleInput);
% Plot membership functions
subplot(3,1,1)
plotmf(fis, 'input', 1)
subplot(3,1,2)
plotmf(fis, 'input', 2)
subplot(3,1,3)
plotmf(fis, 'output',1)
2 Comments
Walter Roberson
on 5 Feb 2026 at 20:06
There used to be a bug report about that message, https://www.mathworks.com/support/bugreports/1465379?s_tid=answers_rc2-2_p5_MLT that was related to savefig()
More Answers (2)
Sam Chak
on 5 Feb 2026 at 3:44
Hi @Michael
If you prefer to use the symbolic approach (==, =>) rather than the linguistic approach (If–Then) as demonstrated by @Walter Roberson, do not use the reserved fuzzy rule-related terms such as "If" and "Then". In your case, when you specified "If HOU_ml-hr == Low", MATLAB interprets this as if you had inadvertently used the phrase "If HOU_ml-hr" as a single variable name. I have made the corrections below, as well as slightly "beautified" the plots to prevent overlapping linguistic names.
If you like the explanation and the enhanced plots, please consider voting 👍 for this Answer.
% Create a Fuzzy Inference System (FIS) with the name "IFR"
fis = mamfis('Name', 'IFR');
% Input variable "MAP"
fis = addInput(fis, [0 200], 'Name', 'MAP_mm');
fis = addMF(fis, "MAP_mm", "trapmf", [ 0 0 55 75], 'Name', "Low");
fis = addMF(fis, "MAP_mm", "trapmf", [ 55 75 100 120], 'Name', "Normal");
fis = addMF(fis, "MAP_mm", "trapmf", [ 100 120 200 200], 'Name', "High");
% Input variable "HOU"
fis = addInput(fis, [0 200], 'Name', 'HOU_ml-hr');
fis = addMF(fis, "HOU_ml-hr", "trapmf", [ 0 0 30 40], 'Name', "Low");
fis = addMF(fis, "HOU_ml-hr", "trapmf", [ 30 40 100 125], 'Name', "Normal");
fis = addMF(fis, "HOU_ml-hr", "trapmf", [ 100 125 200 200], 'Name', "High");
% Output variable "IFR"
fis = addOutput(fis, [0 2000], 'Name', 'IFR_ml-hr');
fis = addMF(fis, "IFR_ml-hr", "trapmf", [ 0 0 60 100], 'Name', "Low");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [ 0 100 200 400], 'Name', "Maintain");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [ 200 400 600 800], 'Name', "Moderate");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [ 600 800 1000 1500], 'Name', "High");
fis = addMF(fis, "IFR_ml-hr", "trapmf", [1000 1500 2000 2000], 'Name', "Very_High");
% Create Rules for the FIS
% r1 = "If HOU_ml-hr==Low & MAP_mm==Low => IFR_ml-hr==Very_High";
% r2 = "If HOU_ml-hr==Normal & MAP_mm==Low => IFR_ml-hr==High";
% r3 = "If HOU_ml-hr==High & MAP_mm==Low => IFR_ml-hr==Moderate";
% r4 = "If HOU_ml-hr==Low & MAP_mm==Normal => IFR_ml-hr==Moderate";
% r5 = "If HOU_ml-hr==Low & MAP_mm==High => IFR_ml-hr==Low";
% r6 = "If HOU_ml-hr==Normal & MAP_mm==Normal => IFR_ml-hr==Maintain";
% r7 = "If HOU_ml-hr==Normal & MAP_mm==High => IFR_ml-hr==Low";
% r8 = "If HOU_ml-hr==High & MAP_mm==Normal => IFR_ml-hr==Maintain";
% r9 = "If HOU_ml-hr==High & MAP_mm==High => IFR_ml-hr==Low";
r1 = "HOU_ml-hr==Low & MAP_mm==Low => IFR_ml-hr==Very_High";
r2 = "HOU_ml-hr==Normal & MAP_mm==Low => IFR_ml-hr==High";
r3 = "HOU_ml-hr==High & MAP_mm==Low => IFR_ml-hr==Moderate";
r4 = "HOU_ml-hr==Low & MAP_mm==Normal => IFR_ml-hr==Moderate";
r5 = "HOU_ml-hr==Low & MAP_mm==High => IFR_ml-hr==Low";
r6 = "HOU_ml-hr==Normal & MAP_mm==Normal => IFR_ml-hr==Maintain";
r7 = "HOU_ml-hr==Normal & MAP_mm==High => IFR_ml-hr==Low";
r8 = "HOU_ml-hr==High & MAP_mm==Normal => IFR_ml-hr==Maintain";
r9 = "HOU_ml-hr==High & MAP_mm==High => IFR_ml-hr==Low";
all = [r1 r2 r3 r4 r5 r6 r7 r8 r9];
fis = addRule(fis, all);
% Evaluate the FIS for inputs [MAP HOU]
% sampleInput = [110 120; 60 25; 30 150; 180 90];
% out = evalfis(fis, sampleInput);
% Plot membership functions
figure
subplot(3,1,1)
plotmf(fis, 'input', 1)
ax1 = gca;
delete(findall(ax1, 'type', 'text'))
text( 25, 1.2, 'Low');
text( 77, 1.2, 'Normal');
text(155, 1.2, 'High');
ylim([-0.2, 1.4])
ylabel({'$\mu$'}, 'interpreter', 'latex')
xlabel({'MAP / mm'}, 'interpreter', 'latex')
subplot(3,1,2)
plotmf(fis, 'input', 2)
ax2 = gca;
delete(findall(ax2, 'type', 'text'))
text( 10, 1.2, 'Low');
text( 60, 1.2, 'Normal');
text(155, 1.2, 'High');
ylim([-0.2, 1.4])
ylabel({'$\mu$'}, 'interpreter', 'latex')
xlabel({'HOU / ml-hr'}, 'interpreter', 'latex')
subplot(3,1,3)
% plotmf(fis, 'output', 3) % there is no Output number 3
plotmf(fis, 'output', 1)
ax3 = gca;
delete(findall(ax3, 'type', 'text'))
text( 0, 1.2, 'Low');
text( 40, 0.8, 'Maintain');
text( 365, 1.2, 'Moderate');
text( 830, 0.8, 'High');
text(1600, 1.2, 'Very High');
ylim([-0.2, 1.4])
ylabel({'$\mu$'}, 'interpreter', 'latex')
xlabel({'IFR / ml-hr'}, 'interpreter', 'latex')
sgtitle("Membership functions for MAP, HOU, and IFR", 'fontsize', 12)
figure
opt = gensurfOptions('NumGridPoints', 51);
gensurf(fis, opt);
ax4 = gca;
delete(findall(ax4, 'type', 'text'))
xlabel({'MAP / mm'}, 'interpreter', 'latex')
ylabel({'HOU / ml-hr'}, 'interpreter', 'latex')
zlabel({'IFR / ml-hr'}, 'interpreter', 'latex')
[az, el] = view()
view(az+180, el)
title("Surface of Human Decision for IFR")
See Also
Categories
Find more on Fuzzy Logic in Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

