📄 fuzzy.m
字号:
set(oldCurrVar, ...
'LineWidth',1, ...
'XColor','black','YColor','black');
% Clear all current variable display registers ...
varTypeHndl=findobj(figNumber,'Type','uicontrol','Tag','currvartype');
set(varTypeHndl,'String',' ');
varNameHndl=findobj(figNumber,'Type','uicontrol','Tag','currvarname');
set(varNameHndl,'String',' ','Enable','off');
varRangeHndl=findobj(figNumber,'Type','uicontrol','Tag','currvarrange');
set(varRangeHndl,'String',' ');
rmvarMenuHndl=findobj(figNumber,'Type','uimenu','Tag','removevar');
set(rmvarMenuHndl,'Enable','off');
% Ensure plot has been redrawn correctly
refresh(figNumber);
watchoff(figNumber)
elseif strcmp(action,'#varname'),
%====================================
figNumber=watchon;
oldfis=get(figNumber,'UserData');
fis=oldfis{1};
currVarAxes=findobj(figNumber,'Type','axes','XColor',selectColor);
varIndex=get(currVarAxes,'UserData');
tag=get(currVarAxes,'Tag');
if strcmp(tag(1:5),'input'),
varType='input';
else
varType='output';
end
varNameHndl=findobj(figNumber,'Type','uicontrol','Tag','currvarname');
newName=deblank(get(varNameHndl,'String'));
% Strip off the leading space
newName=fliplr(deblank(fliplr(newName)));
% Replace any remaining blanks with underscores
newName(find(newName==32))=setstr(95*ones(size(find(newName==32))));
set(varNameHndl,'String',[' ' newName]);
msgStr=['Renaming ' varType ' variable ' num2str(varIndex) ' to "' newName '"'];
statmsg(figNumber,msgStr);
% Change the name of the label in the input-output diagram
txtHndl=get(currVarAxes,'XLabel');
set(txtHndl,'String',newName);
eval(['fis.' varType '(' num2str(varIndex) ').name=''' newName ''';']);
pushundo(figNumber,fis); %%strcmp does not work for structures
updtfis(figNumber,fis,[2 3 4 5]);
watchoff(figNumber);
elseif strcmp(action,'#methodchange'),
%====================================
figNumber=watchon;
mthHndl=gco;
oldfis=get(figNumber,'UserData');
fis=oldfis{1};
mthList=get(mthHndl,'String');
numMth=size(mthList,1);
% The UserData for the popup will tell which method is being
% changed, e.g. andmethod, ormethod, impmethod, etc.
mthName=get(mthHndl,'Tag');
newMthValue=get(mthHndl,'Value');
newMth=deblank(mthList(newMthValue,:));
newMth=fliplr(deblank(fliplr(newMth)));
tempStr=['fis.' mthName];
oldMth=eval(tempStr);
oldMthValue=findrow(oldMth,mthList);
if strcmp(newMth,'Custom...'),
% Handle customized methods...
mthName2=strrep(mthName,'method',' method');
tString=['Adding customized ' mthName2];
statmsg(figNumber,tString);
cmthdlg(figNumber,fis,mthName);
% In case there's a Cancel, return to the old value
set(mthHndl,'Value',oldMthValue);
elseif newMthValue~=oldMthValue,
% Only change things if the method has actually changed
mthName2=strrep(mthName,'method',' method');
tString=['Changing ' mthName2 ' to "' newMth '"'];
statmsg(figNumber,tString);
eval(['fis.' mthName '=''' newMth ''';']);
% Handle undo
pushundo(figNumber,fis)
updtfis(figNumber,fis,[4 5]);
% set(figNumber,'UserData',fis);
end
% statmsg(figNumber,'Ready');
watchoff(figNumber);
elseif strcmp(action,'#openruleedit'),
%====================================
figNumber=watchon;
oldfis=get(figNumber,'UserData');
fis=oldfis{1};
if strcmp(get(figNumber,'SelectionType'),'open'),
% Open the Rule Editor
fisName=fis.name;
guiName='Rule Editor';
newFigNumber=findobj(0,'Name',[guiName ': ' fisName]);
if ~isempty(newFigNumber),
statmsg(figNumber,['Updating ' guiName]);
figure(newFigNumber);
ruleedit('#update');
else
statmsg(figNumber,['Opening ' guiName]);
ruleedit(fis);
end
end
watchoff(figNumber)
elseif strcmp(action,'#plotfis'),
%====================================
figNumber=gcf;
oldfis=get(figNumber,'UserData');
fis=oldfis{1};
set(figNumber,'Nextplot','replace')
if isfield(fis, 'input')
numInputs=length(fis.input);
else
numInputs=0;
end
if isfield(fis, 'output')
numOutputs=length(fis.output);
else
numOutputs=0;
end
numInputMFs=0;
for k=1:numInputs,
numInputMFs=numInputMFs+length(fis.input(k));
end;
numOutputMFs=0;
for k=1:numOutputs,
numOutputMFs=numOutputMFs+length(fis.output(k));
end;
numRules=length(fis.rule);
ruleList=getfis(fis, 'ruleList');
fisName=fis.name;
fisType=fis.type;
mainAxHndl=gca;
set(mainAxHndl,'Units','pixel','Visible','off')
mainAxPos=get(mainAxHndl,'Position');
axis([mainAxPos(1) mainAxPos(1)+mainAxPos(3) ...
mainAxPos(2) mainAxPos(2)+mainAxPos(4)]);
xCenter=mainAxPos(1)+mainAxPos(3)/2;
yCenter=mainAxPos(2)+mainAxPos(4)/2;
axList=[];
if get(0,'ScreenDepth')>2,
inputColor=[1 1 0.5];
outputColor=[0.5 1 1];
else
inputColor=[1 1 1];
outputColor=[1 1 1];
set(gcf,'Color',[1 1 1])
end
% For plotting three cartoon membership functions in the box
xMin=-1; xMax=1;
x=(-1:0.1:1)';
y1=exp(-(x+1).^2/0.32); y2=exp(-x.^2/0.32); y3=exp(-(x-1).^2/0.32);
xlineMatrix=[x x x];
ylineMatrix=[y1 y2 y3];
% Inputs first
fontSize=8;
boxWid=(1/3)*mainAxPos(3);
xInset=boxWid/5;
if numInputs>0,
boxHt=(1/(numInputs))*mainAxPos(4);
yInset=boxHt/5;
end
for varIndex=1:numInputs,
boxLft=mainAxPos(1);
boxBtm=mainAxPos(2)+mainAxPos(4)-boxHt*varIndex;
axPos=[boxLft+xInset boxBtm+yInset boxWid-2*xInset boxHt-2*yInset];
% Draw the line that connects the input to the main block
axes(mainAxHndl);
% Make it a dotted line if the variable is not used in the rule base
if numRules==0,
lineStyle='--';
elseif ~any((ruleList(:,varIndex))),
lineStyle='--';
else
lineStyle='-';
end
xInputCenter=axPos(1)+axPos(3);
yInputCenter=axPos(2)+axPos(4)/2;
line([xInputCenter xCenter],[yInputCenter yCenter], ...
'LineStyle',lineStyle, ...
'LineWidth',2, ...
'Color','black');
% Now draw the little arrowhead on the line
% perpSlope=(xInputCenter-xCenter)/(yCenter-yInputCenter);
% arrowPt=[(xCenter+xInputCenter)/2 (yCenter+yInputCenter)/2];
% delta=(xCenter-xInputCenter)/10;
% line([xArrowPt xArrowPt
varName=fis.input(varIndex).name;
axName=['input' num2str(varIndex)];
axHndl=axes( ...
'Units','pixel', ...
'Box','on', ...
'XTick',[],'YTick',[], ...
'XLim',[xMin xMax],'YLim',[-0.1 1.1], ...
'Color',inputColor, ...
'Tag',axName, ...
'UserData',varIndex, ...
'Position',axPos);
axList=[axList axHndl];
line(xlineMatrix,ylineMatrix,'Color','black');
xiInset=(xMax-xMin)/10;
axis([xMin-xiInset xMax+xiInset -0.1 1.1])
% Lay down a patch that simplifies clicking on the region
patchHndl=patch([xMin xMax xMax xMin],[0 0 1 1],'black');
set(patchHndl, ...
'EdgeColor','none', ...
'FaceColor','none', ...
'UserData',struct('Index',varIndex,'Type','input','Handle',axHndl), ...
'ButtonDownFcn', {@localSelectVar figNumber selectColor});
% Now put on the variable name as a label
% xlabel([varName ' (' num2str(numInputMFs(varIndex)) ')']);
xlabel(varName);
labelName=[axName 'label'];
set(get(axHndl,'XLabel'), ...
'FontSize',fontSize, ...
'Color','black', ...
'Tag',labelName);
end
% Now for the outputs
if numOutputs>0,
boxHt=(1/(numOutputs))*mainAxPos(4);
yInset=boxHt/5;
end
for varIndex=1:numOutputs,
boxLft=mainAxPos(1)+2*boxWid;
boxBtm=mainAxPos(2)+mainAxPos(4)-boxHt*varIndex;
axPos=[boxLft+xInset boxBtm+yInset boxWid-2*xInset boxHt-2*yInset];
% Draw the line connect the center block to the output
axes(mainAxHndl);
% Make it a dotted line if the variable is not used in the rule base
if numRules==0,
lineStyle='--';
elseif ~any(ruleList(:,varIndex+numInputs)),
lineStyle='--';
else
lineStyle='-';
end
line([axPos(1) xCenter],[axPos(2)+axPos(4)/2 yCenter], ...
'LineWidth',2, ...
'LineStyle',lineStyle, ...
'Color','black');
varName=fis.output(varIndex).name;
axName=['output' num2str(varIndex)];
axHndl=axes( ...
'Units','pixel', ...
'Box','on', ...
'Color',outputColor, ...
'XTick',[],'YTick',[], ...
'Tag',axName, ...
'UserData',varIndex, ...
'Position',axPos);
%set(axHndl,'UserData',struct('Index',varIndex,'Handle',axHndl);
axList=[axList axHndl];
if ~strcmp(fisType,'sugeno'),
% Don't try to plot outputs it if it's a Sugeno-style system
x=[-1 -0.5 0 0.5 1]';
xlineMatrix=[x x x];
ylineMatrix=[0 1 0 0 0;0 0 1 0 0; 0 0 0 1 0]';
line(xlineMatrix,ylineMatrix,'Color','black');
xoInset=(xMax-xMin)/10;
axis([xMin-xoInset xMax+xoInset -0.1 1.1])
else
set(axHndl,'XLim',[xMin xMax],'YLim',[-0.1 1.1])
text(0,0.5,'f(u)', ...
'FontSize',fontSize, ...
'Color','black', ...
'HorizontalAlignment','center');
end
% Lay down a patch that simplifies clicking on the region
patchHndl=patch([xMin xMax xMax xMin],[0 0 1 1],'black');
set(patchHndl, ...
'EdgeColor','none', ...
'FaceColor','none', ...
'UserData',struct('Index',varIndex,'Type','output','Handle',axHndl), ...
'ButtonDownFcn', {@localSelectVar figNumber selectColor});
% xlabel([varName ' (' num2str(numOutputMFs(varIndex)) ')']);
xlabel(varName);
labelName=[axName 'label'];
set(get(axHndl,'XLabel'), ...
'FontSize',fontSize, ...
'Color','black', ...
'Tag',labelName);
end
% Now draw the box in the middle
boxLft=mainAxPos(1)+boxWid;
boxBtm=mainAxPos(2);
boxHt=mainAxPos(4);
yInset=boxHt/4;
axPos=[boxLft+xInset boxBtm+yInset boxWid-2*xInset boxHt-2*yInset];
axHndl=axes( ...
'Units','pixel', ...
'Box','on', ...
'XTick',[],'YTick',[], ...
'YLim',[-1 1],'XLim',[-1 1], ...
'XColor','black','YColor','black', ...
'LineWidth',2, ...
'ButtonDownFcn','fuzzy #openruleedit', ...
'Color','white', ...
'Position',axPos);
axList=[axList axHndl];
text(0,1/3,fisName, ...
'Tag','fisname', ...
'FontSize',fontSize, ...
'ButtonDownFcn','fuzzy #openruleedit', ...
'Color','black', ...
'HorizontalAlignment','center');
text(0,-1/3,['(' fisType ')'], ...
'FontSize',fontSize, ...
'ButtonDownFcn','fuzzy #openruleedit', ...
'Color','black', ...
'HorizontalAlignment','center');
% text(0,-1/2,[num2str(numRules) ' rules'], ...
% 'ButtonDownFcn','fuzzy #openruleedit', ...
% 'FontSize',fontSize, ...
% 'Color','black', ...
% 'HorizontalAlignment','center');
set(get(axHndl,'Title'),'FontSize',fontSize,'Color','black');
for count=1:length(axList),
axes(axList(count));
end
set(figNumber,'HandleVisibility','callback')
hndlList=findobj(figNumber,'Units','pixels');
set(hndlList,'Units','normalized')
% Ensure plot has been redrawn correctly
refresh(figNumber);
elseif strcmp(action,'#help');
%====================================
figNumber=watchon;
helpwin('fuzzy/fuzzy/fuzzy');
watchoff(figNumber)
end; % if strcmp(action, ...
%%%%%%%%%%%%%%%%%%%%
% localSelectVar %
%%%%%%%%%%%%%%%%%%%%
function localSelectVar(eventSrc, eventData, figNumber, selectColor)
% This used to be called using elseif strcmp(action,'#selectvar')
% Function is called on initialization of the fuzzy editor and
% when user btn's down on, or deletes an input or output variable.
figX=watchon;
oldfis=get(figNumber,'UserData');
fis=oldfis{1};
if ishandle(eventSrc)
% Function was called via callback from btn down on a variables patch
info = get(eventSrc, 'UserData');
newCurrVar = info.Handle; % axes where patch is drawn
varIndex = info.Index;
varType = info.Type;
% Reset any selected items
kids = findobj(figNumber,'Type','Axes','XColor',selectColor);
set(kids,'LineWidth',1,'XColor','k','YColor','k');
else
% Function was called at initialization of a new GUI or variable, therefore
varType = eventSrc; % A string of the variable type to select
numVars = eventData; % A string of the variable index number to select
kids = get(figNumber,'children');
newCurrVar = findobj(kids,'tag', [varType numVars]);
varIndex = str2num(numVars);
% Plot will have already been redrawn therefore no need to reset
end
% Ensure plot has been redrawn correctly
refresh(figNumber);
% If there are no variables left to plot dont try to plot them
if varIndex ~= 0
% Now highlight the new selection
set(newCurrVar,'XColor',selectColor,'YColor',selectColor,'LineWidth',3);
% Set all current variable display registers ...
varNameHndl=findobj(figNumber,'Type','uicontrol','Tag','currvarname');
varRangeHndl=findobj(figNumber,'Type','uicontrol','Tag','currvarrange');
if strcmp(varType, 'input'),
set(varNameHndl,'String',[' ' fis.input(varIndex).name],'Enable','on');
set(varRangeHndl,'String',mat2str(fis.input(varIndex).range));
else
set(varNameHndl,'String',[' ' fis.output(varIndex).name],'Enable','on');
set(varRangeHndl,'String',mat2str(fis.output(varIndex).range));
end
varTypeHndl=findobj(figNumber,'Type','uicontrol','Tag','currvartype');
set(varTypeHndl,'String',varType);
rmvarMenuHndl=findobj(figNumber,'Type','uimenu','Tag','removevar');
set(rmvarMenuHndl,'Enable','on')
if strcmp(get(figNumber,'SelectionType'),'open'),
% Open the MF Editor with the right variable in view,
% when user double clicks on variable.
fisName=fis.name;
guiName='Membership Function Editor';
newFigNumber=findobj(0,'Name',[guiName ': ' fisName]);
if ~isempty(newFigNumber),
statmsg(figNumber,['Updating ' guiName]);
figure(newFigNumber);
mfedit('#update',varType,varIndex);
else
statmsg(figNumber,['Opening ' guiName]);
mfedit(fis,varType,varIndex);
end
end
end
watchoff(figX)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -