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

📄 mfdrag.m

📁 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱
💻 M
📖 第 1 页 / 共 3 页
字号:
function [out1, out2] = mfdrag(action, lineHndl, mfType, mfParam);
% MFDRAG Interactive changes of membership functions by clicking and dragging.
%   It is called from mfedit.m where action is 'mf' and lineHndl is the current
%   selected membership function.  MFDRAG allows clicking and dragging of 
%   membership functions to change their shapes. In general, clicking on a 
%   membership function curve translates the whole curve; clicking on square 
%   control points changes the shape.
%
%       File name: mfdrag.m
%
%       See also DSIGMF, GAUSSMF, GAUSS2MF, GBELLMF, EVALMF, PIMF, PSIGMF,
%       SIGMF, SMF, TRAPMF, TRIMF, and ZMF.

%       Roger Jang, 6-28-95, Kelly Liu 3-12-97
%       Copyright (c) 1994-98 by The MathWorks, Inc.
% $Revision: 1.9 $



selectColor=[1 0 0];
switch action
 case 'mf',
        lineH=lineHndl;
        set(lineH, 'Color', 'red');
        x = get(lineH, 'xdata');
        mf_type = mfType;
        mf_param = mfParam;
        new_mf = evalmf(x, mf_param, mf_type); 
        set(lineH, 'ydata', new_mf, 'color', 'red');
        % setting control points and mouse actions
        feval(mfilename, 'set_control_point', lineH, mf_type, mf_param);

 case 'get_current_mf',
        mf_type = str2mat('trimf', 'trapmf', 'gbellmf', 'gaussmf', 'gauss2mf',...
                          'sigmf', 'dsigmf',  'psigmf', 'pimf',...
                          'smf', 'zmf');
        mf_type = str2mat(mf_type);

 %       mf_param_n = [3 4 2 2 3 2 2 4 4 4 4];
        which_mf = get(findobj(gcf, 'tag', 'mftype'), 'value');
        out1 = mf_type(which_mf, :);
        out2=[];
 case 'set_control_point',
        % delete all possible previous control points
        delete(findobj(gcf, 'tag', 'leftlow'));
        delete(findobj(gcf, 'tag', 'lefthigh'));
        delete(findobj(gcf, 'tag', 'center'));
        delete(findobj(gcf, 'tag', 'righthigh'));
        delete(findobj(gcf, 'tag', 'rightlow'));
        % find MF types and parameters
        mf_type = mfType;
        mf_param = mfParam;
        square = local_get_control_square;
        controlcolor='black';
        if strcmp(mf_type, 'trimf'),
                leftlowH = line(mf_param(1)+real(square), imag(square), ...
                        'color', controlcolor, 'tag', 'leftlow');
                centerH = line(mf_param(2)+real(square), 1+imag(square), ...
                        'color', controlcolor, 'tag', 'center');
                rightlowH = line(mf_param(3)+real(square), imag(square), ...
                        'color', controlcolor, 'tag', 'rightlow');
        elseif strcmp(mf_type, 'trapmf') | strcmp(mf_type, 'pimf'),
                leftlowH = line(mf_param(1)+real(square), imag(square), ...
                        'color', controlcolor, 'tag', 'leftlow');
                lefthighH = line(mf_param(2)+real(square), 1+imag(square), ...
                        'color', controlcolor, 'tag', 'lefthigh');
                righthighH = line(mf_param(3)+real(square), 1+imag(square), ...
                        'color', controlcolor, 'tag', 'righthigh');
                rightlowH = line(mf_param(4)+real(square), imag(square), ...
                        'color', controlcolor, 'tag', 'rightlow');
        elseif strcmp(mf_type, 'gaussmf'),
                sigma = mf_param(1);
                c = mf_param(2);
                height = 0.5;
                x_left = c - sigma*sqrt(-2*log(height));
                x_right = c + sigma*sqrt(-2*log(height));
                leftlowH = line(x_left+real(square), height+imag(square), ...
                        'color', controlcolor, 'tag', 'leftlow');
                rightlowH = line(x_right+real(square), height+imag(square), ...
                        'color', controlcolor, 'tag', 'rightlow');
        elseif strcmp(mf_type, 'gauss2mf'),
                s1 = mf_param(1); c1 = mf_param(2);
                s2 = mf_param(3); c2 = mf_param(4);
                height = 0.5;
                x_left = c1 - s1*sqrt(-2*log(height));
                x_right = c2 + s2*sqrt(-2*log(height));
                leftlowH = line(x_left+real(square), height+imag(square), ...
                        'color', controlcolor, 'tag', 'leftlow');
                rightlowH = line(x_right+real(square), height+imag(square), ...
                        'color', controlcolor, 'tag', 'rightlow');
                lefthighH = line(mf_param(2)+real(square), 1+imag(square), ...
                        'color', controlcolor, 'tag', 'lefthigh');
                righthighH = line(mf_param(4)+real(square), 1+imag(square), ...
                        'color', controlcolor, 'tag', 'righthigh');
        elseif strcmp(mf_type, 'sigmf'),
                a = mf_param(1);
                c = mf_param(2);
                height = 0.1;
                leftlowH = line(c-log(1/height-1)/a + real(square), ...
                        height + imag(square), ...
                        'color', controlcolor, 'tag', 'leftlow');
                righthighH = line(c-log(1/(1-height)-1)/a + real(square), ...
                        1-height + imag(square), ...
                        'color', controlcolor, 'tag', 'righthigh');
        elseif strcmp(mf_type, 'gbellmf'),
                a = mf_param(1); b = mf_param(2); c = mf_param(3);
                height = 0.9;
                leftlowH = line(c-a+real(square), 0.5+imag(square), ...
                        'color', controlcolor, 'tag', 'leftlow');
                rightlowH = line(c+a+real(square), 0.5+imag(square), ...
                        'color', controlcolor, 'tag', 'rightlow');
                lefthighH = line(c-a*(1/height-1)^(1/(2*b))+real(square), ...
                        height+imag(square), 'color', controlcolor, 'tag', 'lefthigh');
                righthighH = line(c+a*(1/height-1)^(1/(2*b))+real(square), ...
                        height+imag(square), 'color', controlcolor, 'tag', 'righthigh');
        elseif strcmp(mf_type, 'smf'),
                leftlowH = line(mf_param(1)+real(square), imag(square), ...
                        'color', controlcolor, 'tag', 'leftlow');
                righthighH = line(mf_param(2)+real(square), 1+imag(square), ...
                        'color', controlcolor, 'tag', 'righthigh');
        elseif strcmp(mf_type, 'zmf'),
                lefthighH = line(mf_param(1)+real(square), 1+imag(square), ...
                        'color', controlcolor, 'tag', 'lefthigh');
                rightlowH = line(mf_param(2)+real(square), imag(square), ...
                        'color', controlcolor, 'tag', 'rightlow');
        elseif strcmp(mf_type, 'psigmf'),
                a1=mf_param(1); c1=mf_param(2); a2=mf_param(3); c2=mf_param(4);
                height = 0.9;
                leftlowH = line(c1+real(square), 0.5+imag(square), ...
                        'color', controlcolor, 'tag', 'leftlow');
                rightlowH = line(c2+real(square), 0.5+imag(square), ...
                        'color', controlcolor, 'tag', 'rightlow');
                lefthighH = line(c1-log(1/height-1)/a1+real(square), ...
                        height+imag(square), ...
                        'color', controlcolor, 'tag', 'lefthigh');
                righthighH = line(c2-log(1/height-1)/a2+real(square), ...
                        height+imag(square), ...
                        'color', controlcolor, 'tag', 'righthigh');
        elseif strcmp(mf_type, 'dsigmf'),
                a1=mf_param(1); c1=mf_param(2); a2=mf_param(3); c2=mf_param(4);
                height = 0.9;
                leftlowH = line(c1+real(square), 0.5+imag(square), ...
                        'color', controlcolor, 'tag', 'leftlow');
                rightlowH = line(c2+real(square), 0.5+imag(square), ...
                        'color', controlcolor, 'tag', 'rightlow');
                lefthighH = line(c1-log(1/height-1)/a1+real(square), ...
                        height+imag(square), ...
                        'color', controlcolor, 'tag', 'lefthigh');
                righthighH = line(c2+log(1/height-1)/a2+real(square), ...
                        height+imag(square), ...
                        'color', controlcolor, 'tag', 'righthigh');
        else
             msgStr='not a default mf type';
             statHndl=findobj(gcf, 'Tag', 'status');
             set(statHndl, 'String', msgStr);
             return
      %          fprintf('mf_type = %s\n', mf_type);
      %          error('Unknown MF type!');
        end

        % === set mouse action
        % action when button is first pushed down
        action1 = [mfilename '(''mouse_action1'')'];

        % actions after the mouse is pushed down
        action2 = [mfilename '(''mouse_action2'')'];
        % action when button is released
        action3 = [mfilename '(''mouse_action3'')'];

        % temporary storage for the recall in the down_action
        param=get(gca, 'UserData');
        param.Action=action2;
        set(gca,'UserData',param);
     
        % set action when the mouse is pushed down
        
        down_action = [...
                'a=get(gca, ''UserData'');'...
                'if isstruct(a)&isfield(a, ''Action''), set(gcf,''WindowButtonMotionFcn'',a.Action);' action1 '; end; '];
        set(gcf,'WindowButtonDownFcn',down_action);

        % set action when the mouse is released
        up_action = [ ...
                'set(gcf,''WindowButtonMotionFcn'','' '');', action3];
        set(gcf,'WindowButtonUpFcn', up_action);
 case 'mouse_action1',
        curr_info = get(gca, 'CurrentPoint');
        userparam=get(gca, 'Userdata');
        leftlowH = findobj(gcf, 'tag', 'leftlow');
        lefthighH = findobj(gcf, 'tag', 'lefthigh');
        centerH = findobj(gcf, 'tag', 'center');
        righthighH = findobj(gcf, 'tag', 'righthigh');
        rightlowH = findobj(gcf, 'tag', 'rightlow');
        lineH = findobj(gcf, 'tag', 'mfline', 'Userdata', userparam.CurrMF);
        allH = [leftlowH lefthighH centerH righthighH rightlowH lineH];
        set(allH, 'erasemode', 'xor');
        % test if inside control squares
        for i = 1:length(allH)-1,
      
                set(allH(i), 'userdata', ...
                        inside(curr_info(1,1)+j*curr_info(1,2), ...
                        get(allH(i),'xdata')'+j*get(allH(i),'ydata')'));
        end
        % test if clicking on the line
        if gco == lineH,
                param = eval(get(findobj(gcf, 'tag', 'mfparams'), 'string'));
                set(findobj(gcf, 'tag', 'mfparams'), 'userdata', [curr_info(1,1) param]);
        end;

 case 'mouse_action2',
        curr_info = get(gca, 'CurrentPoint');

        leftlowH = findobj(gcf, 'tag', 'leftlow');
        rightlowH = findobj(gcf, 'tag', 'rightlow');
        lefthighH = findobj(gcf, 'tag', 'lefthigh');
        righthighH = findobj(gcf, 'tag', 'righthigh');
        centerH = findobj(gcf, 'tag', 'center');
        mainAxes = findobj(gcf, 'Tag','mainaxes');

        paramLine = get(mainAxes, 'UserData');
        lineH = findobj(gcf, 'tag', 'mfline', 'UserData', paramLine.CurrMF);
        if ~isempty(lineH)
 
          mf_type = deblank(feval(mfilename, 'get_current_mf'));
          square = local_get_control_square;

          paramH = findobj(gcf, 'tag', 'mfparams');
          x = get(lineH, 'xdata');

          mf_type = deblank(feval(mfilename, 'get_current_mf'));
  
          paramstr=get(paramH, 'string');
          if ~isempty(deblank(paramstr))
           param = eval(paramstr);
          else
           param=[];
          end

       % Is the current variable input or output?
          currVarAxes=findobj(gcf,'Type','axes','XColor',selectColor);
          varIndex=get(currVarAxes,'UserData');

⌨️ 快捷键说明

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