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

📄 mfedit.m

📁 交流 模糊控制 交流 模糊控制
💻 M
📖 第 1 页 / 共 4 页
字号:
        % Deselect the remove mf menu item
		hndl=findobj(figNumber,'Type','uimenu','Tag','removemf');
		set(hndl,'Enable','off');
	end
	
	watchoff(figNumber)    
	
elseif strcmp(action,'#removeallmfs'),
	%====================================
	figNumber=watchon;
	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');
	mainAxes=findobj(figNumber,'Type','axes','Tag','mainaxes');
	
	lineHndlList=findobj(mainAxes,'Tag','mfline');
	txtHndlList=findobj(mainAxes,'Type','text');
	
	deleteFlag=1;
	count=eval(['length(fis.' varType '(' num2str(varIndex) ').mf)']);
	while count>=1,
		[fis,errorStr]=rmmf(fis,varType,varIndex,'mf',count);
		count=count-1;
		if isempty(fis),
			% if any of these MFs are used in the rule base, we can't delete
			deleteFlag=0;
			statmsg(figNumber,errorStr);
			count=0;
		end
	end
	if deleteFlag
		delete(lineHndlList);
		delete(txtHndlList);
		pushundo(figNumber,fis);
		updtfis(figNumber,fis,[]);
		mfedit #plotmfs
        % Deselect the remove mf menu items
		hndl=findobj(figNumber,'Type','uimenu','Tag','removeallmf');
		set(hndl,'Enable','off');
		hndl=findobj(figNumber,'Type','uimenu','Tag','removemf');
		set(hndl,'Enable','off');
	end
	
	watchoff(figNumber)    
	
elseif strcmp(action,'#addcustommf'),
	%====================================
	figNumber=watchon;
	oldfis=get(figNumber,'UserData');
	fis=oldfis{1};
	% Find the selected variable and MF
	currVarAxes=findobj(figNumber,'Type','axes','XColor',selectColor);
	varIndex=get(currVarAxes,'UserData');
	varType=get(currVarAxes,'Tag');
	
	cmfdlg(figNumber,fis,varType,varIndex,figNumber);
	watchoff(figNumber);
	
elseif strcmp(action,'#addmfs'),
	%====================================
	figNumber=watchon;
	oldfis=get(figNumber,'UserData');
	fis=oldfis{1};
	% Find the selected variable and MF
	currVarAxes=findobj(figNumber,'Type','axes','XColor',selectColor);
	varIndex=get(currVarAxes,'UserData');
	varType=get(currVarAxes,'Tag');
    
    mfdlg(figNumber,fis,varType,varIndex);
    mfdlgfig=findobj(0, 'Tag', 'mfdlg');
    waitfor(mfdlgfig);
    watchoff(figNumber);
    oldfis=get(figNumber,'UserData');
    fis=oldfis{1};
    updtfis(figNumber,fis,[3]);
    % There is now atleast one mf therefore check the remove all mf menu item
    hndl=findobj(figNumber,'Type','uimenu','Tag','removeallmf');
    set(hndl,'Enable','on');
    
elseif strcmp(action,'#plotvars'),
	%====================================
	figNumber=gcf;
	oldfis=get(figNumber,'UserData');
	fis=oldfis{1};
	
	if isfield(fis, 'input')
		numInputs=length(fis.input);
	else
		numInputs=0;
	end
	if isfield(fis, 'output')
		numOutputs=length(fis.output);
	else
		numOutputs=0;
	end
	
	for i=1:numInputs
		if isfield(fis.input(i), 'mf')
			numInputMFs(i)=length(fis.input(i).mf);
		else
			numInputMFs(i)=0;
		end
	end
	
	for i=1:numOutputs
		if isfield(fis.output(i), 'mf')
			numOutputMFs(i)=length(fis.output(i).mf);
		else
			numOutputMFs(i)=0;
		end
	end
	if isfield(fis, 'rule')
		numRules=length(fis.rule);
	else
		numRules=0;
	end
	fisName=fis.name;
	fisType=fis.type;
	
	mainAxHndl=gca;
	set(mainAxHndl,'Units','pixel','XTick',[],'YTick',[])
	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;
	title('FIS Variables')
	set(get(mainAxHndl,'Title'),'Visible','on','FontSize',10,'Color','black')
	
	% Inputs first
	
	if get(0,'ScreenDepth')>2,
		inputColor=[1 1 0.5];
		outputColor=[0.5 1 1];
	else
		inputColor=[1 1 1];
		outputColor=[1 1 1];
	end
	
	tickColor=[0.5 0.5 0.5];
    fontSize=8;
    
    boxWid=(1/2)*mainAxPos(3);
    boxHt=(1/(max(4,numInputs)))*mainAxPos(4);
    xInset=boxWid/10;
    yInset=boxHt/5;
    
    xMin=-1; xMax=1;
    
    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];
        
        varName=eval(['fis.input(' num2str(varIndex) ').name']);
        axName='input';
        axHndl=axes( ...
            'Units','pixel', ...
            'Box','on', ...
            'XTick',[],'YTick',[], ...
            'XColor',tickColor,'YColor',tickColor, ...
            'YLim',[-0.1 1.1], ...
            'Color',inputColor, ...
            'Tag',axName, ...
            'UserData',varIndex, ...
            'Position',axPos);
        mfIndex=(1:numInputMFs(varIndex))+sum(numInputMFs(1:(varIndex-1)));
        colorOrder=get(gca,'ColorOrder');
        
        % Plot three cartoon membership functions in the box
        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];
        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', ...
            'ButtonDownFcn','mfedit #selectvar');
        
        % Now put on the variable name as a label
        xlabel(varName);
        labelName=[axName 'label'];
        set(get(axHndl,'XLabel'), ...
            'FontSize',fontSize, ...
            'Color','black', ...
            'Tag',labelName); 
    end
    
    % Now for the outputs
    boxHt=(1/(max(4,numOutputs)))*mainAxPos(4);
    
    
    
    for varIndex=1:numOutputs,
        boxLft=mainAxPos(1)+boxWid;
        boxBtm=mainAxPos(2)+mainAxPos(4)-boxHt*varIndex;
        axPos=[boxLft+xInset boxBtm+yInset boxWid-2*xInset boxHt-2*yInset];
        
        varName=eval(['fis.output(' num2str(varIndex) ').name']);
        axName='output';
        axHndl=axes( ...
            'Units','pixel', ...
            'Box','on', ...
            'Color',outputColor, ...
            'XTick',[],'YTick',[], ...  
            'XLim',[xMin xMax],'YLim',[-0.1 1.1], ...
            'XColor',tickColor,'YColor',tickColor, ...
            'Tag',axName, ...
            'UserData',varIndex, ...
            'Position',axPos);
        mfIndex=(1:numOutputMFs(varIndex))+sum(numOutputMFs(1:(varIndex-1)));
        if ~strcmp(fisType,'sugeno'),
            % Only try to plot outputs it if it's not 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;
        else
            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', ...
            'ButtonDownFcn','mfedit #selectvar');
        
        xlabel(varName);
        labelName=[axName 'label'];
        set(get(axHndl,'XLabel'), ...
            'FontSize',fontSize, ...
            'Color','black', ...
            'Tag',labelName);
    end
    
    hndlList=findobj(figNumber,'Units','pixels');
    set(hndlList,'Units','normalized')
    
elseif strcmp(action,'#help');
    figNumber=watchon;
    helpwin(mfilename)
    watchoff(figNumber)
    
end;    % if strcmp(action, ...


%%%%%%%%%%%%%%%%%%%%%%%
%%% localgetmfparam %%%
%%%%%%%%%%%%%%%%%%%%%%%
function out = localgetmfparam(fis, varType, varNum, mfNum, param)
switch varType
case 'input'
    switch param
    case 'name'
        out=fis.input(varNum).mf(mfNum).name;
    case 'type'
        out=fis.input(varNum).mf(mfNum).type;
    case 'params'
        out=fis.input(varNum).mf(mfNum).params;
    end   
case 'output'
    switch param
    case 'name'
        out=fis.output(varNum).mf(mfNum).name;
    case 'type'
        out=fis.output(varNum).mf(mfNum).type;
    case 'params'
        out=fis.output(varNum).mf(mfNum).params;
    end   
end


%%%%%%%%%%%%%%%%%%%%%
%   localSelectmf   %
%%%%%%%%%%%%%%%%%%%%%
function localSelectmf(eventSrc, eventHdl, selectColor)
% Called when a user btn downs on an mf and selects the first mf
% to be selected when a new input or output is plotted.

figNumber=watchon;
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 ~isempty(eventSrc)
    % The function was called from a btn down action on an object
    mainAxes=get(eventSrc,'Parent');	
    % Find the new current MF
    param=get(mainAxes,'UserData');
    oldCurrMF=param.CurrMF;
    newCurrMF=get(eventSrc,'UserData');
    param.CurrMF=newCurrMF;
    plot_mfs = 1;
else
    % The function was called directly and should initialize the first mf
    oldCurrMF = -1;
    % Check there is an mf to plot
    if ~isempty(currVarAxes)
        % Check there is an mf to plot
        if length(eval(['fis.' varType '(' num2str(varIndex) ').mf'])) >= 1
            plot_mfs = 1;
            % Set the first mf to be selected
            newCurrMF = 1;
            kids = get(figNumber,'Children');
            mainAxes = kids(find(strcmp(get(kids,'tag'),'mainaxes')));
            param.CurrMF=1;
        else
            % There are no mfs therefore dont plot anything
            plot_mfs = 0;
            % Set the remove mf menuitems
            h1 = findobj(figNumber,'Type','uimenu','Tag','removemf');
            h2 = findobj(figNumber,'Type','uimenu','Tag','removeallmf');
            set([h1, h2],'Enable','off');
        end
    else 
        % There is no selected input or output therefore dont plot anything
        plot_mfs = 0;
    end
end

% Plot an mf
if plot_mfs == 1
    % Deselect other currently selected MF curves
    if oldCurrMF~=newCurrMF, 
        if oldCurrMF~=-1 
            mfedit #deselectmf
        end
        set(mainAxes,'UserData',param);
        
        % Find the info for the new MF
        mfType=localgetmfparam(fis, varType, varIndex, newCurrMF, 'type');
        mfName=localgetmfparam(fis, varType, varIndex, newCurrMF, 'name');
        mfParams=localgetmfparam(fis, varType, varIndex, newCurrMF, 'params');      
        % Set the MF name, type and params to the right value
        mfNameHndl=findobj(figNumber,'Type','uicontrol','Tag','mfname');
        set(mfNameHndl,'String',[' ' mfName],'Enable','on');
        
        mfTypeHndl=findobj(figNumber,'Type','uicontrol','Tag','mftype');
        mfTypeList=get(mfTypeHndl,'String');
        if strcmp(fisType,'sugeno') & strcmp(varType,'output'),
            % Prepare sugeno mf type popup menu
            if size(mfTypeList,1)>2,
                set(mfTypeHndl,'String',get(mfTypeHndl,'UserData'));
                set(mfTypeHndl,'UserData',mfTypeList);
            end
        else
            % Prepare mamdani mf type popup menu
            if size(mfTypeList,1)==2,
                set(mfTypeHndl,'String',get(mfTypeHndl,'UserData'));
                set(mfTypeHndl,'UserData',mfTypeList);
            end
            % Make the selected line bold
            currLineHndl=findobj(mainAxes,'Tag','mfline','UserData',newCurrMF);
            
            set(currLineHndl,'Color',selectColor);
            mfdrag('mf', currLineHndl, mfType, mfParams);
            set(currLineHndl,'LineWidth',2);
        end
        
        % Make the selected text bold
        currTxtHndl=findobj(mainAxes,'Type','text','UserData',newCurrMF);
        set(currTxtHndl,'Color',selectColor,'FontWeight','bold');
        
        mfTypeList=get(mfTypeHndl,'String');
        mfTypeValue=findrow(mfType,mfTypeList);
        if isempty(mfTypeValue),
            mfTypeList=str2mat(mfTypeList, [' ' mfType]);
            mfTypeValue=findrow(mfType,mfTypeList);
            set(mfTypeHndl,'String',mfTypeList,'Value',mfTypeValue);
            msgStr=['Installing custom membership function "' mfType '"'];
            statmsg(figNumber,msgStr);
        end
        set(mfTypeHndl,'Value',mfTypeValue,'Enable','on');
        curr_info = get(gca, 'CurrentPoint');
        
        hndl=findobj(figNumber, 'Tag','mfparams');
        set(hndl,'String',[' ' mat2str(mfParams,4)],'Enable','on', ...
            'Userdata', [curr_info(1,1) mfParams]);
        
        hndl=findobj(figNumber,'Type','uimenu','Tag','removemf');
        set(hndl,'Enable','on');
        
    end
    % Reset the remove all mfs menuitem
    hndl=findobj(figNumber,'Type','uimenu','Tag','removeallmf');
    set(hndl,'Enable','on');
end
watchoff(figNumber)

⌨️ 快捷键说明

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