📄 anfisedit.m
字号:
chkvector(i)=chk_err(1);
set(chkHndl, 'Ydata', chkvector);
end
errvector(i)=trn_err(1);
if ~isempty(errHndl)
set(errHndl, 'Ydata', errvector);
drawnow
end
txtStr={['Epoch ' num2str(i) ':error= ' num2str(trn_err(1))]};
set(txtHndl, 'String', txtStr);
stopflag=get(stopHndl, 'String');
if strcmp(stopflag, 'Train Now')| trn_err(1)<=errlim
break;
end
end %end of if Satrt
title('Training Error');
set(testHndl, 'Enable', 'on');
set(stopHndl, 'String', 'Train Now');
else
%=========reset start button=========
set(stopHndl, 'String', 'Train Now');
end
% end of for loop
if ~isempty(chkvector)
trnedfis=fismat2;
else
trnedfis=fismat1;
end
trnedfis.trndata=fis.trndata;
if isfield(fis, 'tstdata');
trnedfis.tstdata=fis.tstdata;
end
if isfield(fis, 'chkdata');
trnedfis.chkdata=fis.chkdata;
end
% set(gcf, 'UserData', trnedfis);
figNumber=watchon;
%===========updata fis for all the fis editors=======
updtfis(figNumber,trnedfis,[2 3 4 5 6]);
pushundo(gcbf, trnedfis);
watchoff;
end
%========take training input data from commandline workspace
case '#trndatin',
fis=get(gcf, 'UserData');
fis=fis(1);
trndatinHndl=findobj(gcf, 'Tag', 'trndatin');
trndatinTxt=get(trndatinHndl, 'String');
trnData=[];
trnData=evalin('base', trndatinTxt, '[]');
if isempty(trnData),
msgbox('No such variable (or variable is empty)')
else
fis.trndata(:,1:length(fis.input))=trnData
end
%========take training output data from commandline workspace
case '#genfis',
figNumber=gcbf;
genHndl=findobj(figNumber, 'Tag', 'genfis');
n=get(genHndl, 'Value');
indexStr='';
for i=1:length(n)
if n{i}~=0
indexStr=get(genHndl(i), 'String');
break;
end
end
oldfis=get(gcf,'UserData');
fis=oldfis{1};
if ~isfield(fis, 'trndata');
trnData=[];
else
trnData=fis.trndata;
end
fismat=[];
param=[];
switch indexStr
case 'Grid partition'
%========use grid genfis===========
if isempty(trnData)
msgbox('Load training data in order to generate ANFIS');
break;
end
mfType=getfis(fis, 'inmftypes');
dlgFig=findobj('type', 'figure', 'Tag', 'genfis1dlg');
if isempty(dlgFig)
param=gfmfdlg('#init', fis);
else
figure(dlgFig);
end
if ~isempty(param)
mfType=char(param(2));
outType=char(param(3));
inmflist=str2num(char(param(1)));
if ~isempty(inmflist)&length(inmflist)~=length(fis.input)
inmflist(end+1:length(fis.input))=inmflist(end);
end
if isempty(inmflist)
inmflist=[2];
end
if isempty(mfType)
% for i=1:length(fis.input)
% mfType(i,:)='gbellmf';
% end
mfType='gbellmf';
end
fismat=genfis1(trnData, inmflist, mfType, outType);
%in case user changed the following from mfedit or fiseditor
fismat.type=fis.type;
fismat.name=fis.name;
fismat.andMethod = fis.andMethod;
fismat.orMethod = fis.orMethod;
fismat.defuzzMethod =fis.defuzzMethod;
fismat.impMethod = fis.impMethod;
fismat.aggMethod = fis.aggMethod;
end
case 'Sub. clustering'
%========use cluster genfis=====================
if isempty(trnData)
msgbox('Load training data in order to generate ANFIS');
break;
end
param=inputdlg({'Range of influence:', 'Squash factor:','Accept ratio:','Reject ration:'},...
'Parameters for clustering genfis', 1,...
{'.5', '1.25', '.5', '.15'});
if ~isempty(param)
watchon;
fismat=genfis2(trnData(:,1:length(fis.input)), trnData(:,length(fis.input)+1), str2num(param{1}),[], [str2num(param{2}), str2num(param{3}), str2num(param{4}), 0]);
fismat.name=fis.name;
watchoff
end
case 'Load from disk'
[fname, fpath]=uigetfile('*.fis');
if fname ~=0
fismat=readfis([fpath fname]);
%make sure loading a sugeno type fis structure
if ~strcmp(fismat.type, 'sugeno')
msgbox('Not a sugeno type, no fis structure is loaded');
fismat=[];
end
end
case 'Load from worksp.'
vname=[];
vname=inputdlg('input variable name');
if ~isempty(vname)
fismat=evalin('base', char(vname), '[]');
end
if isempty(fismat),
msgbox('No such variable (or variable is empty)')
else
if ~strcmp(fismat.type, 'sugeno')
msgbox('Not a sugeno type, no fis structure is loaded');
fismat=[];
end
end
end
if ~isempty(fismat)
if isfield(fis, 'tstdata')
fismat.tstdata=fis.tstdata;
end
if isfield(fis, 'chkdata')
fismat.chkdata=fis.chkdata;
end
set(figNumber, 'Name',['Anfis Editor: ' fismat.name]);
fismat.trndata=trnData;
textHndl=findobj(gcbf, 'Tag', 'Comments');
textstr={['# of input: ' num2str(length(fismat.input))], ['# of outputs: ' ...
num2str(length(fismat.output))], ['# of input mfs: '], [num2str(getfis(fismat, 'inmfs'))]};
set(textHndl, 'String', textstr);
cmtHndl=findobj(gcbf, 'Tag', 'status');
set(cmtHndl, 'String', 'a new fis generated');
% set(figNumber, 'Userdata', fismat);
%========updata all the fis editors===============
% set(textHndl, 'String', 'New fis generated');
pushundo(figNumber, fismat);
updtfis(figNumber,fismat,[2 3 4 5]);
LocalEnableBtns(fismat);
end
%=======mouse down function for plotting structure=========
case '#mousedownstr'
patchHndl=gco;
showStr=get(patchHndl, 'Tag');
showPosx = get(patchHndl, 'XData');
showPosy = get(patchHndl, 'YData');
textHndl=findobj(gcf, 'Tag', 'strcparam');
set(textHndl,'String', showStr, 'Position', [showPosx(1)*.75+.08 showPosy(1)*.75+.05 .2 .040], 'Visible', 'on');
%=======plot anfis structure=========
case '#plotstruc',
fis=get(gcbf, 'Userdata');
fis=fis{1};
plotFig=findobj('type', 'figure', 'Name', 'Anfis Model Structure');
if isempty(plotFig)
%==========make sure only one figure plot exists=============
plotFig=figure('Name', 'Anfis Model Structure',...
'Unit', 'normal',...
'WindowButtonDownFcn', 'anfisedit #showparam',...
'WindowButtonUpFcn', 'anfisedit #clearparam',...
'NumberTitle','off',...
'Tag', 'plotstruc');
uicontrol('Style', 'pushbutton',...
'Unit', 'normal',...
'Position', [.1 .01 .15 .05],...
'Callback', 'close(gcf)', 'String', 'Close');
uicontrol('Style', 'text',...
'Unit', 'normal',...
'Position', [.3 .01 .5 .05],...
'String', 'Click on each node to see detailed information');
uicontrol('Style', 'text', 'Unit', 'normal',...
'Visible', 'on',...
'Position', [0.18 0.9 .7 .05],...
'Max', 1,...
'String', 'input inputmf rule outputmf weighted sum output output');
axis equal
axis off;
end
gcf=plotFig;
TextHndl=uicontrol('Style', 'text', 'Unit', 'normal',...
'Visible', 'off',...
'Position', [0.1 0 .1 .005], 'Tag', 'strcparam');
plotFigChild=get(plotFig, 'children');
axHndl=[];
%=======makesure to plot on the right axes========
for i=1:length(plotFigChild)
if strcmp(get(plotFigChild(i), 'type'),'axes')
axHndl = plotFigChild(i);
end
end
if ~isempty(axHndl)
axes(axHndl);
end
set(gca, 'XLimMode', 'Manual', 'Xlim', [-0.10 1.1], 'Ylim', [-0.1 1.0]);
pos=get(plotFig, 'Position');
%========input nodes and input mfs
instep=1/(length(fis.input)+1);
outstep=1/(length(fis.output)+1);
theta=0:pi/5:2*pi;
r=.02;
rsin=r*sin(theta);
rcos=r*cos(theta);
%=========rules and output mfs
rulestep=1/(length(fis.rule)+1);
rulecolor={'blue', 'red'};
for i=1:length(fis.output)
for j=1:length(fis.rule)
line([.6 .8], [rulestep*j outstep*i], 'color', 'black');
end
end
for i=1:length(fis.rule)
conn=fis.rule(i).connection;
for j=1:length(fis.rule(i).antecedent)
ruleindex=fis.rule(i).antecedent(j);
if ruleindex~=0
if conn==-1
thiscolor='green';
else
thiscolor=rulecolor(conn);
end
mfstep=instep/(length(fis.input(j).mf)+1);
line([.2 .4], [(j-1/2)*instep+mfstep*ruleindex rulestep*i], 'color', char(thiscolor));
end
end
temp=rulestep*i;
line([.4 .6], [temp temp], 'color', 'black');
line([.4 .6], [temp .01], 'color', 'black');
xcircle=r*sin(theta)+.4;
ycircle=r*cos(theta)+temp;
patch(xcircle, ycircle, 'w',...
'ButtonDownFcn', 'anfisedit #mousedownstr',...
'Tag', ['rule ' num2str(i)]);
xcircle=xcircle+.2;
patch(xcircle, ycircle, 'w',...
'ButtonDownFcn', 'anfisedit #mousedownstr',...
'Tag', ['output mf ' num2str(fis.rule(i).consequent(1))]);
end
for i=1:length(fis.input)
mfstep=instep/(length(fis.input(i).mf)+1);
for j=1:length(fis.input(i).mf)
xcircle=rsin+.2;
ycircle=rcos+instep*(i-1/2)+mfstep*j;
line([0 .2], [instep*i (i-1/2)*instep+mfstep*j], 'color', 'black');
patch(xcircle, ycircle, 'w',...
'ButtonDownFcn', 'anfisedit #mousedownstr',...
'Tag', ['input ' num2str(i) ' mf ' fis.input(i).mf(j).name]);
end
xcircle=rsin;
ycircle=rcos+instep*i;
patch(xcircle, ycircle, 'black', 'ButtonDownFcn', 'anfisedit #mousedownstr', 'Tag', ['input ' num2str(i)]);
end
%=====output nodes
line([.8 1], [outstep outstep/2], 'color', 'black');
line([.6 1], [.01 outstep/2],'color', 'black');
for i=1:length(fis.output)
xcircle=rsin+.8;
ycircle=rcos+outstep*i;
patch(xcircle, ycircle, 'w',...
'ButtonDownFcn', 'anfisedit #mousedownstr',...
'Tag', ['weighted sum output ' num2str(i)]);
end
%=======normalize and dividing node
xcircle=rsin+.6;
ycircle=rcos+.01;
patch(xcircle, ycircle, 'r',...
'ButtonDownFcn', 'anfisedit #mousedownstr',...
'Tag', ['normalization factor ']);
xcircle=rsin+1;
ycircle=rcos+outstep/2;
patch(xcircle, ycircle, 'black',...
'ButtonDownFcn', 'anfisedit #mousedownstr',...
'Tag', ['output ']);
line ([.9 1.3], [0.5 0.5], 'Color', 'blue');
text(1.17, .49, 'and');
line ([.9 1.3], [.6 0.6], 'Color', 'red');
text(1.17, .59, 'or');
line ([.9 1.3], [0.7 0.7], 'Color', 'green');
text(1.17, .69, 'not');
%============== mouse up function for plotting structure===============
case '#clearparam',
plotFig=findobj('type', 'figure', 'Name', 'Anfis Model Structure');
textHndl=findobj(plotFig, 'Tag', 'strcparam');
set(textHndl, 'Visible', 'off');
%========open training set file================
case '#opentrn',
% open an existing file
oldfis=get(gcbf,'UserData');
fis=oldfis{1};
typeHndl=findobj(gcbf, 'Tag', 'dattype');
fromHndl=findobj(gcbf, 'Tag', 'trndatin');
for i=1:length(typeHndl)
if get(typeHndl(i), 'Value')==1
thistype=get(typeHndl(i), 'String');
switch thistype
case 'Testing',
type ='test';
case 'Training',
type='train';
case 'Checking',
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -