📄 mfedit.m
字号:
if strcmp(varType,'input'),
backgroundColor=inputColor;
else
backgroundColor=outputColor;
end
set(lineHndl,'Color',backgroundColor);
set(lineHndl,'LineWidth',1);
set(lineHndl,'Color',unselectColor);
% set(lineHndl, 'Tag', 'line');
set(txtHndl,'Color',unselectColor,'FontWeight','normal');
% Clean up the MF fields
hndl=findobj(figNumber,'Type','uicontrol','Tag','mfname');
if strcmp(get(hndl,'Enable'),'on'),
set(hndl,'String',' ','Enable','off');
hndl=findobj(figNumber,'Tag','mftype');
set(hndl,'Value',1,'Enable','off');
hndl=findobj(figNumber,'Type','uicontrol','Tag','mfparams');
set(hndl,'String',' ','Enable','off');
hndl=findobj(figNumber,'Type','uimenu','Tag','removemf');
set(hndl,'Enable','off');
end
elseif strcmp(action,'#varrange'),
%====================================
figNumber=watchon;
oldfis=get(figNumber,'UserData');
fis=oldfis{1};
currVarAxes=findobj(figNumber,'Type','axes','XColor',selectColor);
varIndex=get(currVarAxes,'UserData');
varType=get(currVarAxes,'Tag');
% Get the range
oldRange=eval(['fis.' varType '(' num2str(varIndex) ').range']);
varRangeHndl=findobj(figNumber,'Type','uicontrol','Tag','varrange');
dispRangeHndl=findobj(figNumber,'Type','uicontrol','Tag','disprange');
newRangeStr=get(varRangeHndl,'String');
newRangeStr=get(varRangeHndl,'String');
% We'll put the brackets in later; no point in dealing with the hassle
index=[find(newRangeStr=='[') find(newRangeStr==']')];
newRangeStr(index)=32*ones(size(index));
newRangeStr=['[' newRangeStr ']'];
% Use eval try-catch to prevent really weird stuff...
newRange=eval(newRangeStr,mat2str(oldRange,4));
if length(newRange)~=2,
statmsg(figNumber,'Range vector must have exactly two elements');
newRange=oldRange;
end
if diff(newRange)<=0,
statmsg(figNumber,'Range vector must be of the form [lowLimit highLimit]');
newRange=oldRange;
end
rangeStr=mat2str(newRange,4);
set(varRangeHndl,'String',[' ' rangeStr]);
if ~all(newRange==oldRange),
% Don't bother to do anything unless it's changed
% Change all params here
numMFs=eval(['length(fis.' varType '(' num2str(varIndex) ').mf)']);
for count=1:numMFs,
oldParams=eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(count) ').params']);
mfType=eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(count) ').type']);
[newParams,errorStr]=strtchmf(oldParams,oldRange,newRange,mfType);
eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(count) ').params=' mat2str(newParams) ';']);
end
eval(['fis.' varType '(' num2str(varIndex) ').range=' mat2str(newRange) ';']);
updtfis(figNumber,fis,[4 5]);
pushundo(figNumber,fis);
% ... and plot
set(dispRangeHndl,'String',[' ' rangeStr]);
mfedit #plotmfs
end
watchoff(figNumber)
elseif strcmp(action,'#disprange'),
%====================================
figNumber=watchon;
oldRange=[];
oldfis=get(figNumber,'UserData');
fis=oldfis{1};
% Find current variable
currVarAxes=findobj(figNumber,'Type','axes','XColor',selectColor);
varIndex=get(currVarAxes,'UserData');
varType=get(currVarAxes,'Tag');
varRangeHndl=findobj(figNumber,'Type','uicontrol','Tag','varrange');
dispRangeHndl=findobj(figNumber,'Type','uicontrol','Tag','disprange');
% Get the old range
oldRangeStr=get(dispRangeHndl,'UserData');
newRangeStr=get(dispRangeHndl,'String');
% We'll put the brackets in later; no point in dealing with the hassle
index=[find(newRangeStr=='[') find(newRangeStr==']')];
newRangeStr(index)=32*ones(size(index));
newRangeStr=['[' newRangeStr ']'];
% Use eval try-catch to prevent really weird stuff...
newRange=eval(newRangeStr,mat2str(oldRange,4));
if length(newRange)~=2,
statmsg(figNumber,'Range vector must have exactly two elements');
newRangeStr=oldRangeStr;
end
if diff(newRange)<=0,
statmsg(figNumber,'Range vector must be of the form [lowLimit highLimit]');
newRangeStr=oldRangeStr;
end
newRange=eval(newRangeStr,oldRangeStr);
rangeStr=mat2str(newRange,4);
set(dispRangeHndl,'String',[' ' rangeStr]);
% ... and plot
mfedit #plotmfs
watchoff(figNumber)
elseif strcmp(action,'#mfname'),
%====================================
figNumber=watchon;
oldfis=get(figNumber,'UserData');
fis=oldfis{1};
mfNameHndl=findobj(figNumber,'Type','uicontrol','Tag','mfname');
currVarAxes=findobj(figNumber,'Type','axes','XColor',selectColor);
varIndex=get(currVarAxes,'UserData');
varType=get(currVarAxes,'Tag');
if strcmp(varType,'input'),
backgroundColor=inputColor;
else
backgroundColor=outputColor;
end
thisuserdata=get(gca,'UserData');
currMF=thisuserdata.CurrMF;
oldName=eval(['fis.' varType '(' num2str(varIndex),').mf(' num2str(currMF),').name']);
newName=deblank(get(mfNameHndl,'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))));
msgStr=['Renaming MF ' num2str(currMF) ' to "' newName '"'];
statmsg(figNumber,msgStr);
txtHndl=findobj(figNumber,'Type','text','UserData',currMF);
set(txtHndl,'Color',backgroundColor);
set(txtHndl,'String',newName);
set(txtHndl,'Color',selectColor);
set(mfNameHndl,'String',[' ' newName]);
eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').name=''' newName '''' '; ']);
pushundo(figNumber,fis);
updtfis(figNumber,fis,[3 4]);
watchoff(figNumber)
elseif strcmp(action,'#mftype'),
%====================================
figNumber=watchon;
% mfTypeHndl=get(figNumber,'CurrentObject');
mfTypeHndl=gcbo;
oldfis=get(figNumber,'UserData');
fis=oldfis{1};
fisType=fis.type;
numInputs=length(fis.input);
% Is the current variable input or output?
currVarAxes=findobj(figNumber,'Type','axes','XColor',selectColor);
varIndex=get(currVarAxes,'UserData');
varType=get(currVarAxes,'Tag');
mainAxes=findobj(figNumber,'Type','axes','Tag','mainaxes');
% currMF=get(mainAxes,'UserData');
param=get(mainAxes,'UserData');
currMF=param.CurrMF;
if strcmp(varType,'input'),
backgroundColor=inputColor;
else
backgroundColor=outputColor;
end
typeList=get(mfTypeHndl,'String');
typeValue=get(mfTypeHndl,'Value');
newType=deblank(typeList(typeValue,:));
% Strip off the leading space
newType=fliplr(deblank(fliplr(newType)));
msgStr=['Changing MF ' num2str(currMF) ' type to "' newType '"'];
statmsg(figNumber,msgStr);
paramHndl=findobj(figNumber,'Tag','mfparams');
% Now translate and insert the translated parameters
if strcmp(fisType,'sugeno') & strcmp(varType,'output'),
% Handle the sugeno case
oldParams=eval(['fis.' varType '(' num2str(varIndex) ').mf(',num2str(currMF) ').params']);
if strcmp(newType,'constant'),
% Pick off only the constant term
newParams=oldParams(length(oldParams));
eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').type=''' newType '''' ';']);
eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').params=' mat2str(newParams) ';']);
else
eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').type=''' newType '''' ';']);
if length(oldParams)==1
eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').params=' '[' mat2str(zeros(1, numInputs)) ' ' mat2str(oldParams) ']' ';']);
end
end
newParams=eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').params']);
mfParamHndl=findobj(figNumber,'Type','uicontrol','Tag','mfparams');
set(mfParamHndl,'String',[' ' mat2str(newParams,4)]);
pushundo(figNumber,fis);
updtfis(figNumber,fis,[4 5]);
else
oldParams=eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').params']);
oldType=eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').type']);
oldType=deblank(oldType);
newType=deblank(newType);
[newParams,errorStr]=mf2mf(oldParams,oldType,newType);
if isempty(newParams),
statmsg(figNumber,errorStr);
set(paramHndl,'String',[' ' mat2str(oldParams,4)]);
val=findrow(oldType,typeList);
set(mfTypeHndl,'Value',val);
else
% Set the MF params to the right value
set(paramHndl,'String',[' ' mat2str(newParams,4)]);
% Replot the new curve
lineHndl=findobj(mainAxes,'Tag','mfline','UserData',currMF);
% lineHndlList=findobj(mainAxes,'Type','mfline');
% for i=1:length(lineHndlList)
% thisparam=get(lineHndlList(i), 'UserData');
% if ~isempty(thisparam) & thisparam.CurrMF == currMF,
% lineHndl=lineHndlList(i);
% break;
% end
% end
txtHndl=findobj(mainAxes,'Type','text','UserData',currMF);
% First erase the old curve
set(lineHndl,'Color',backgroundColor);
set(txtHndl,'Color',backgroundColor);
x=get(lineHndl,'XData');
mfType=eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').type']);
y=evalmf(x,newParams,newType);
set(lineHndl,'YData',y,'Color',selectColor);
centerIndex=find(y==max(y));
centerIndex=round(mean(centerIndex));
txtPos=get(txtHndl,'Position');
txtPos(1)=x(centerIndex);
set(txtHndl,'Position',txtPos,'Color',selectColor);
eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').type=''' newType ''';']);
eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').params=' mat2str(newParams) ';']);
pushundo(figNumber,fis);
updtfis(figNumber,fis,[4 5]);
mfdrag('mf', lineHndl, newType, newParams);
end
end
watchoff(figNumber)
elseif strcmp(action,'#mfparams'),
%====================================
figNumber=watchon;
% mfParamHndl=get(figNumber,'CurrentObject');
mfParamHndl = gcbo;
oldfis=get(figNumber,'UserData');
fis=oldfis{1};
fisType=fis.type;
% Is the current variable input or output?
currVarAxes=findobj(figNumber,'Type','axes','XColor',selectColor);
varIndex=get(currVarAxes,'UserData');
varType=get(currVarAxes,'Tag');
if strcmp(varType,'input'),
backgroundColor=inputColor;
else
backgroundColor=outputColor;
end
mainAxes=findobj(figNumber,'Type','axes','Tag','mainaxes');
%currMF=get(mainAxes,'UserData');
param=get(mainAxes,'UserData');
currMF=param.CurrMF;
newParamStr=get(mfParamHndl,'String');
if isempty(newParamStr), newParamStr=' '; end
% We'll put the brackets in later; no point in dealing with the hassle
index=[find(newParamStr=='[') find(newParamStr==']')];
newParamStr(index)=32*ones(size(index));
newParamStr=['[' newParamStr ']'];
% Use eval try-catch to prevent really weird stuff...
newParams=eval(newParamStr,'[]');
% Use the old parameters for error-checking
oldParams=eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').params']);
if ~isequal(size(newParams),size(oldParams)),
newParams=oldParams;
set(mfParamHndl,'String',[' ' mat2str(newParams,4)]);
% Send status message to the user
msgStr=['No change made to MF ' num2str(currMF)];
statmsg(figNumber,msgStr);
else
% Send status message to the user
msgStr=['Changing parameter for MF ' num2str(currMF) ' to ' newParamStr];
statmsg(figNumber,msgStr);
set(mfParamHndl,'String',[' ' mat2str(newParams,4)]);
if strcmp(fisType,'sugeno') & strcmp(varType,'output'),
% Nothing to do for sugeno output case...
eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').params=' mat2str(newParams) ';']);
pushundo(figNumber,fis);
updtfis(figNumber,fis,[4 5]);
else
lineHndl=findobj(mainAxes,'Type','line','UserData',currMF);
x=get(lineHndl,'XData');
mfType=eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').type']);
y=eval('evalmf(x,newParams,mfType)','[]');
if isempty(y),
% There's been an error in the MF code, so don't change anything
statmsg(figNumber,['Illegal parameter in membership function ' mfType]);
newParams=oldParams;
set(mfParamHndl,'String',[' ' mat2str(newParams,4)]);
else
% Replot the curve
txtHndl=findobj(mainAxes,'Type','text','UserData',currMF);
set(txtHndl,'Color',backgroundColor);
set(lineHndl,'Color',backgroundColor);
drawnow
drawnow
set(lineHndl,'YData',y);
centerIndex=find(y==max(y));
centerIndex=round(mean(centerIndex));
txtPos=get(txtHndl,'Position');
txtPos(1)=x(centerIndex);
set(txtHndl,'Position',txtPos);
set(lineHndl,'Color',selectColor);
set(txtHndl,'Color',selectColor);
eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(currMF) ').params=' mat2str(newParams) ';']);
pushundo(figNumber,fis);
mfdrag('mf', lineHndl, mfType, newParams);
updtfis(figNumber,fis,[4 5]);
end
end
end
watchoff(figNumber)
elseif strcmp(action,'#plotmfs'),
%====================================
figNumber=gcf;
oldfis=get(figNumber,'UserData');
fis=oldfis{1};
% Find the selected variable
currVarAxes=findobj(figNumber,'Type','axes','XColor',selectColor);
varIndex=get(currVarAxes,'UserData');
varType=get(currVarAxes,'Tag');
if strcmp(varType,'input'),
backgroundColor=inputColor;
else
backgroundColor=outputColor;
end
fisType=fis.type;
mainAxes=findobj(figNumber,'Tag','mainaxes');
axes(mainAxes);
varName=eval(['fis.' varType '(' num2str(varIndex) ').name']);
numMFs=eval(['length(fis.' varType '(' num2str(varIndex) ').mf)']);
if strcmp(fisType,'sugeno') & strcmp(varType,'output'),
% Handle sugeno case
cla
if isfield(fis, 'input')
numInputs=length(fis.input);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -