⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 anfisedit.m

📁 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱
💻 M
📖 第 1 页 / 共 3 页
字号:
         type='check';
       otherwise,
         type='demo';
      end
      break
     end
   end
   inNum=length(fis.input);
   outNum=length(fis.output);
   varname=[];
   if get(fromHndl(2), 'value') ==0 & ~strcmp(type, 'demo')
   %from workspace
     vname=[];
     trndata=[];
     vname=inputdlg('input variable name');
     dtloaded=0;
     if ~isempty(vname)
      trndata=evalin('base', char(vname), '[]');
      if isempty(trndata),
         msgbox('No such variable (or variable is empty)')
      else
         dtloaded=1;
      end   
     end     
   elseif ~strcmp(type, 'demo')
   %from file
      [fname, fpath]=uigetfile('*.dat'); 
      dtloaded=0;
      trndata=[];
      if isstr(fname)&isstr(fpath)
         fid=[fpath fname];
         load(fid);
         dtloaded=1;
         dotIndex=find(fname=='.');
         if ~isempty(dotIndex)
          varname=fname(1:dotIndex-1);
         else
          varname=fname;
         end
         trndata=eval(varname);
       end 
   else
      %demo data
      load('fuzex1trn.dat');
      load('fuzex1chk.dat');
      trndata=fuzex1trn;
      tstdata=fuzex1chk;
      chkdata=[];
      fis=genfis1(trndata, 4, 'gaussmf');
      fis.trndata=trndata;
      fis.tstdata=tstdata;
      fis.chkdata=chkdata;
      dtloaded=1;
   end  
  if ~isempty(trndata) & ~strcmp(type, 'demo'),
   if size(trndata, 2)<=1
       msgbox('Data needs to be at least two columes! No data is loaded.')
       clear trndata;
       statueHndl=findobj(gcbf, 'Tag', 'status');
       set(statueHndl, 'String', [type ' data is not loaded']);
       return;
   end
   inNumNew=size(trndata,2)-1;
   if inNumNew~=inNum 
           if ~isempty(fis.input(1).mf) | strcmp(type,'test')==1 | strcmp(type,'check')==1
             msgbox({['The number of inputs for ' type ' data is ' num2str(inNumNew)],...
                            ['The number of inputs for current fuzzy system is ' num2str(inNum)],...
                            ['No new ' type ' data is loaded']});
             qustout='No';
           else
             qustout='Yes';
           end
           if strcmp(qustout, 'Yes')==1
              dtloaded=1;
              if inNumNew>inNum
               for i=inNum+1:inNumNew
                fis=addvar(fis,'input',['input' num2str(i)],[0 1]);
               end
              else
               for i=inNum:-1:inNumNew+1
                fis=rmvar(fis,'input', i);
               end 
              end
              inNum=length(fis.input);
           else
              dtloaded=0;
              clear trndata;
              statueHndl=findobj(gcbf, 'Tag', 'status');
              
              set(statueHndl, 'String', 'Training data is not loaded');
           end
    end
   end
   if dtloaded==1
          lineMarker=['o', '.', '+'];
          colorIndex=1;
          titleStr='Demo Data';
          switch type
            case 'train'
              fis.trndata=trndata;
              titleStr='Training Data (ooo)';
            case 'test'
              fis.tstdata=trndata;
              titleStr='Testing Data (...)';
              colorIndex=2;
            case 'check'
              fis.chkdata=trndata;
              titleStr='Checking Data (+++)';
              colorIndex=3;
          end 
          updtfis(gcbf,fis,[6]);
          textHndl=findobj(gcbf, 'Tag', 'Comments');
          textstr={['# of inputs: ' num2str(length(fis.input))], ['# of outputs: ' ...
            num2str(length(fis.output))],  ['# of input mfs: '], [ num2str(getfis(fis, 'inmfs'))],...
            ['# of ' type ' data pairs: ' num2str(size(trndata,1))]};
          targetlineHndl=line([1:size(trndata, 1)], trndata(:, inNum+outNum),...
                             'lineStyle', 'none', ...
                             'Marker', lineMarker(colorIndex), 'Tag', 'targetline'); 
          title(titleStr);
          xlabelHndl=get(gca, 'XLabel');
         
          set(xlabelHndl, 'String', 'data set index');
          ylabelHndl=get(gca, 'YLabel');
          set(ylabelHndl, 'String', 'Output');
          set(textHndl, 'String', textstr);
          cmtHndl=findobj(gcbf, 'Tag', 'status');
          set(cmtHndl, 'String', [type ' data loaded']);
          pushundo(gcbf, fis);
    %      set(gcbf, 'UserData', fis); 
         end
        
         LocalEnableBtns(fis);  
      
  %% end
   %=============test anfis==============
case '#test',
   fis=get(gcbf,'UserData');
   fis=fis{1};
   testHndl=findobj(gcbf, 'Tag', 'test');
   cla
   for i=1:length(testHndl)
    if get(testHndl(i), 'Value')==1 
      thistype=get(testHndl(i), 'String');
      testdata=[];
      markerStr='o';
      switch thistype
       case 'Testing data',
         if isfield(fis, 'tstdata')
          testdata=fis.tstdata;
          markerStr='.';
         else
          msgbox([thistype ' does not exist']);          
         end
       case 'Training data',
         if isfield(fis, 'trndata')
          testdata=fis.trndata;
          markerStr='o';
         else
          msgbox([thistype ' does not exist']);
         end
       case 'Checking data',
         if isfield(fis, 'chkdata')
          testdata=fis.chkdata;
          markerStr='+';
         else
          msgbox([thistype ' does not exist']);
         end
       otherwise,
         msgbox([thistype ' does not exist']);
      end
      break
     end
   end
   if ~isempty(testdata)
      xlabelHndl=get(gca, 'XLabel');
      ylabelHndl=get(gca, 'YLabel');
      set(xlabelHndl, 'String', 'Index');
      set(ylabelHndl, 'String', 'Output');
      datasize=size(testdata, 1);
      inputnum=size(testdata, 2)-1;
      targetdata=testdata(1:datasize, inputnum+1);
      testOut=evalfis(testdata(1:datasize, 1:inputnum), fis);
      errordata=sum(abs(targetdata-testOut))/length(targetdata);
      targetlineHndl=line([1:datasize],targetdata,...
                          'lineStyle', 'none', 'Marker', markerStr); 
      title( [thistype ' : ' markerStr '   FIS output : *']);
      testlineHndl=line([1:datasize],testOut, 'lineStyle', 'none', 'Marker', '*', 'Color', 'red'); 
      statueHndl=findobj(gcbf, 'Tag', 'status');
      set(statueHndl, 'String', ['Average testing error: ' num2str(errordata)]);
   else
      msgbox([thistype ' does not exist']);
   end
case '#cleardata'
    oldfis=get(gcbf,'UserData'); 
    fis=oldfis{1};
   typeHndl=findobj(gcbf, 'Tag', 'dattype');
   for i=1:length(typeHndl)
    if get(typeHndl(i), 'Value')==1 
      thistype=get(typeHndl(i), 'String');
      out=questdlg({['do you really want to clear ' thistype ' Data?']}, '', 'Yes', 'No', 'No'); 
      if strcmp(out, 'Yes')
       switch thistype
        case 'Testing',
          fis.tstdata=[];
        case 'Training',
          fis.trndata=[];
        case 'Checking',
          fis.chkdata=[];
       end
       cla
       pushundo(gcbf, fis);
      end
      break
     end
   end
   
case '#radioloadfrom'
      curHndl=gcbo;
      radioHndl=findobj(gcbf, 'Tag', 'trndatin');
      if radioHndl(1)==curHndl
        set(radioHndl(2), 'Value', 0);
        set(radioHndl(1), 'Value', get(radioHndl(1), 'max'));
      else
        set(radioHndl(1), 'Value', 0);
        set(radioHndl(2), 'Value', get(radioHndl(2), 'max'));
      end
case '#radioloadtype'
      curHndl=gco;
      radioHndl=findobj(gcbf, 'Tag', 'dattype');
      set(radioHndl, 'Value', 0);
      set(curHndl, 'Value', 1);
case '#radiogenfis'
      curHndl=gco;
      set(curHndl, 'Value', get(curHndl, 'max'));
      thisstr=get(curHndl, 'String');
      genHndl=findobj(gcbf, 'Tag', 'genfisbtn');
      radioHndl=findobj(gcbf, 'Tag', 'genfis');
      for i=1:length(radioHndl)
       if radioHndl(i)~=curHndl
        set(radioHndl(i), 'Value', 0);
       end
      end
      switch thisstr
       case 'Grid partition'
        set(genHndl, 'String', 'Generate FIS ...');
       case 'Sub. clustering'
        set(genHndl, 'String', 'Generate FIS ...');
       otherwise
        set(genHndl, 'String', 'Load ...');
      end
case '#radiotest'
      curHndl=gco;
      set(curHndl, 'Value', get(curHndl, 'max'));
      radioHndl=findobj(gcbf, 'Tag', 'test');
      for i=1:length(radioHndl)
       if radioHndl(i)~=curHndl
        set(radioHndl(i), 'Value', 0);
       end
      end
case '#help'
   figNumber=watchon;
   helpwin(mfilename)
   watchoff(figNumber)
end;    % if strcmp(action, ...
% End of function anfisedit

%==================================================
function LocalEnableBtns(fis)
% control the enable property for buttons, based on whether training data/mf/rule
% has already set
startHndl = findobj(gcf, 'Tag', 'startbtn');

if ~isfield(fis, 'trndata')| isempty(fis.trndata)| isempty(fis.input(1).mf) 
   %| isempty(fis.input(1).mf)|...
   %      ~isfield(fis, 'rule') | isempty(fis.rule)
   set(startHndl, 'Enable', 'off');
else
   set(startHndl, 'Enable', 'on');
end

%genHndl = findobj(gcf, 'Tag', 'genfis');

%if ~isfield(fis, 'trndata')| isempty(fis.trndata(1))
%   set(genHndl, 'Enable', 'off');
%else
%   set(genHndl, 'Enable', 'on'); 
%end
plotHndl = findobj(gcf, 'Tag', 'plotstrc');
testHndl = findobj(gcf, 'Tag', 'testbtn');
if isfield(fis,'input') & (isempty(fis.input(1).mf) | ~isfield(fis, 'rule') | isempty(fis.rule))
   set(plotHndl, 'Enable', 'off');
   set(testHndl, 'Enable', 'off');
   
else
   set(plotHndl, 'Enable', 'on');
   set(testHndl, 'Enable', 'on');
   
end


%==================================================
function uiHandle=LocalBuildUi(uiPos, uiStyle, uiCallback, promptStr, uiTag)
% build uicontrol 
uiHandle=uicontrol( ...
   'Style',uiStyle, ...
   'HorizontalAlignment','left', ...
   'BackgroundColor',[.75 .75 .75], ...  
   'Units','normalized', ...
   'Max',20, ...
   'Position',uiPos, ...
   'Callback',uiCallback, ... 
   'Tag', uiTag, ...
   'String',promptStr);

%==================================================
function frmHandle=LocalBuildFrmTxt(frmPos, txtStr, uiStyle, txtTag)
% Build frame and label with an edge
%frmColor=192/255*[1 1 1];
frmColor=[.75 .75 .75];
frmHandle=uicontrol( ...
   'Style', uiStyle, ...
   'Units','normalized', ...
   'Position',frmPos, ...
   'HorizontalAlignment', 'left',...
   'BackgroundColor',frmColor, ...
   'String', txtStr, ...
   'Tag', txtTag);
%   'ForegroundColor',[1 1 1], ...                  %generates an edge

%==================================================
function btHandle=LocalBuildBtns(thisstyle, btnNumber, labelStr, callbackStr, uiTag)
% build buttons or check boxes so they easily aline on the right

labelColor=[0.75 0.75 0.75];
top=0.953;
left=0.825;
btnWid=0.15;
btnHt=0.05;
bottom=0.032;
% Spacing between the button and the next command's label
spacing=0.03;

yPos=top-(btnNumber-1)*(btnHt+spacing);
if strcmp(labelStr, 'Close')==1
   yPos= bottom;
elseif strcmp(labelStr, 'Info')==1
   yPos= bottom+btnHt+spacing; 
else
   yPos=top-(btnNumber-1)*(btnHt+spacing)-btnHt;
end
% Generic button information
btnPos=[left yPos btnWid btnHt];
btHandle=uicontrol( ...
   'Style',thisstyle, ...
   'Units','normalized', ...
   'Position',btnPos, ...
   'String',labelStr, ...
   'Tag', uiTag, ...
   'Callback',callbackStr); 

%==================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -