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

📄 mfedit.m

📁 交流 模糊控制 交流 模糊控制
💻 M
📖 第 1 页 / 共 4 页
字号:
	
	% 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=fis.(varType)(varIndex).mf(currMF).params;
		if strcmp(newType,'constant'),
			% Pick off only the constant term
			newParams=oldParams(length(oldParams));
		    fis.(varType)(varIndex).mf(currMF).type=newType;
			fis.(varType)(varIndex).mf(currMF).params=newParams;
		else
			fis.(varType)(varIndex).mf(currMF).type=newType;
			if length(oldParams)==1
				fis.(varType)(varIndex).mf(currMF).params=[zeros(1,numInputs) oldParams];
			end  
		end
		newParams=fis.(varType)(varIndex).mf(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=fis.(varType)(varIndex).mf(currMF).params;
		oldType=fis.(varType)(varIndex).mf(currMF).type;
		oldType=deblank(oldType);
		newType=deblank(newType);
        varRange=fis.(varType)(varIndex).range;
        tol=1e-3*(varRange(2)-varRange(1));
		[newParams,errorStr]=mf2mf(oldParams,oldType,newType,tol);
		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'),
	%====================================
	mfParamHndl = gcbo;
	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 strcmp(varType,'input'),
		backgroundColor=inputColor;
	else 
		backgroundColor=outputColor;
	end
	mainAxes=findobj(figNumber,'Type','axes','Tag','mainaxes');
	param=get(mainAxes,'UserData');
	currMF=param.CurrMF;
	
	% Read user input
	newParamStr=get(mfParamHndl,'String');
	if isempty(newParamStr)
		newParams = [];
	else
		newParams = evalin('base',newParamStr,'[]');
		if ~isreal(newParams)
			newParams = [];
		else
		    newParams = reshape(newParams,[1 prod(size(newParams))]);
		end
	end
	
	% Use the old parameters for error-checking
	RefStruct = substruct('.',varType,'()',{varIndex},'.','mf','()',{currMF});
	EditedMF = subsref(fis,RefStruct);
	oldParams = EditedMF.params;
	
	% Resolve length discrepancies for Sugeno outputs
	if length(newParams)==1 & strcmpi(fisType,'sugeno') & strcmpi(varType,'output')
		newParams = [zeros(1,length(fis.input)*strcmp(EditedMF.type,'linear')),newParams];
	end
			
	% Update FIS data
	if length(newParams)~=length(oldParams),
		% Back out change
		newParams = oldParams;
		msgStr = sprintf('Invalid parameter vector. No change made to MF %d',currMF);
	else
		% Send status message to the user
		msgStr=sprintf('Changing parameter for MF %d to %s',currMF,newParamStr);
		
		if strcmp(fisType,'sugeno') & strcmp(varType,'output'),
			% Nothing to do for sugeno output case...
			EditedMF.params = newParams;
			fis = subsasgn(fis,RefStruct,EditedMF);
			pushundo(figNumber,fis);
			updtfis(figNumber,fis,[4 5]);
		else
			lineHndl=findobj(mainAxes,'Type','line','UserData',currMF);
			x=get(lineHndl,'XData');
			try 
				y = evalmf(x,newParams,EditedMF.type);
				% New data OK. Update FIS
				EditedMF.params = newParams;
				fis = subsasgn(fis,RefStruct,EditedMF);
				% Replot the curve
				txtHndl=findobj(mainAxes,'Type','text','UserData',currMF);
				centerIndex=find(y==max(y));
				centerIndex=round(mean(centerIndex));
				txtPos=get(txtHndl,'Position');
				txtPos(1) = x(centerIndex);
				txtPos(2) = y(centerIndex) + 0.1;
				set(txtHndl,'Position',txtPos,'Color',selectColor);
				set(lineHndl,'YData',y,'Color',selectColor);
				pushundo(figNumber,fis);
				mfdrag('mf', lineHndl, EditedMF.type, newParams);
				updtfis(figNumber,fis,[4 5]);
			catch
				% There's been an error in the MF code, so don't change anything
				msgStr = sprintf('Invalid parameter vector. No change made to MF %s',EditedMF.type);
				newParams=oldParams;
			end
		end
	end
	set(mfParamHndl,'String',[' ' mat2str(newParams,4)]);
	statmsg(figNumber,msgStr);
	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']);
	try
		numMFs=eval(['length(fis.' varType '(' num2str(varIndex) ').mf)']);
	catch
		numMFs=0;
	end  
	if strcmp(fisType,'sugeno') & strcmp(varType,'output'),
		% Handle sugeno case
		cla
		if isfield(fis, 'input')
			numInputs=length(fis.input);
		else
			numInputs=0;
		end
		
		inLabels=[];
		for i=1:numInputs
			inLabels=strvcat(inLabels, fis.input(i).name);
		end
		
		varRange=[-1 1];
		for count=1:numMFs,
			mfName=eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(count) ').name']);
			txtStr=deblank(mfName);
			if numMFs>6,
				% Use two columns
				if (count-1)<(numMFs/2),
					% This is column one
					xPos=0.25*diff(varRange)+varRange(1);
					yPos=(count-1)/(numMFs/2-1);
				else
					% This is column two
					xPos=0.75*diff(varRange)+varRange(1);
					yPos=(count-round(numMFs/2)-1)/(round(numMFs/2)-1);
				end
			else
				% Only one column is necessary
				xPos=0;
				yPos=(count)/(numMFs);
			end
			
			text(xPos,yPos,txtStr, ...
				'Color',unselectColor, ...
				'UserData',count, ...
				'EraseMode','normal', ...
				'HorizontalAlignment','center', ...
				'FontSize',8, ...
				'ButtonDownFcn',{@localSelectmf selectColor});
		end
		set(gca,'XTick',[],'YTick',[], ...
			'XLim',[-1 1],'YLim',[-0.2 1.2], ...
			'Color',backgroundColor);
	else
		% This is either an input variable or a mamdani output
		dispRangeHndl=findobj(figNumber,'Tag','disprange');
		varRange=eval(get(dispRangeHndl,'String'));
		ptsHndl = findobj(figNumber, 'Tag', 'numpoints');
		numPts=get(ptsHndl, 'String');
		numPts=str2double(numPts);
		cla
		% Draw all the lines
		set(gca, ...
			'YTick',[0 0.5 1],'XTickMode','auto', ...
			'YLim',[-0.05 1.2], ...
			'Color',backgroundColor);
		[xAllMFs,yAllMFs]=plotmf(fis,varType,varIndex,numPts);
		for mfIndex=1:numMFs,
			x=xAllMFs(:,mfIndex);
			y=yAllMFs(:,mfIndex);
			mfName=eval(['fis.' varType '(' num2str(varIndex) ').mf(' num2str(mfIndex) ').name']);
			line(x,y, ...
				'Color',unselectColor, ...
				'LineWidth',1, ...
				'UserData',mfIndex, ...
				'EraseMode','normal', ...
				'Tag', 'mfline',...
				'ButtonDownFcn',{@localSelectmf selectColor})
			centerIndex=find(y==max(y));
			centerIndex=round(mean(centerIndex));
			text(x(centerIndex), 1.1 ,mfName, ...
				'HorizontalAlignment','center', ...
				'Color',unselectColor, ...
				'FontSize',8, ...
				'UserData',mfIndex, ...
				'EraseMode','normal', ...
				'Tag', 'mftext',...
				'ButtonDownFcn',{@localSelectmf selectColor});
			param=get(gca, 'Userdata');
			param.CurrMF=-1;
			set(gca,'UserData',param,'XLim',varRange);
			set(gca,'XLim',varRange);
		end
	end
	
	xlabel([varType ' variable "' varName '"'],'Color','black');
	
	% Clean up the MF fields
	hndl=findobj(figNumber,'Type','uicontrol','Tag','mfname');
	set(hndl,'String',' ','Enable','off');
	hndl=findobj(figNumber,'Type','uicontrol','Tag','mftype');
	set(hndl,'Value',1,'Enable','off');
	hndl=findobj(figNumber,'Type','uicontrol','Tag','mfparams');
	set(hndl,'String',' ','Enable','off');

    % Ensure plot is redrawn correctly
    refresh(figNumber);
	
elseif strcmp(action,'#removemf'),
	%====================================
	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');
	mainAxes=findobj(figNumber,'Type','axes','Tag','mainaxes');
	param=get(mainAxes,'UserData');
	%  currMF=get(mainAxes,'UserData');
	currMF=param.CurrMF;
	
	lineHndl=findobj(mainAxes,'Type','line','UserData',currMF);
	
	txtHndl=findobj(mainAxes,'Type','text','UserData',currMF);
	
	[fis,errorStr]=rmmf(fis,varType,varIndex,'mf',currMF, true);
	if isempty(fis),
		statmsg(figNumber,errorStr);
	else
		delete(lineHndl);
		delete(txtHndl);
		updtfis(figNumber,fis,[3 4 5 6]);
		pushundo(figNumber,fis);
		mfedit #plotmfs

⌨️ 快捷键说明

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