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

📄 ruler.m

📁 matlabDigitalSigalProcess内有文件若干
💻 M
📖 第 1 页 / 共 5 页
字号:
         end
         if isempty(ud.focusline)
             return
         end
         xd = get(ud.focusline,'xdata');
         yd = get(ud.focusline,'ydata');
         if isempty(xd)
             return
         end
         
         ind = findpeaks(-yd);
         if isempty(ind)
             % line is a vector of constants (no valleys); unselect valleys
             btnup(fig,'peaksgroup',2)
             return
         end

         set(ud.ruler.lines(5),'xdata',xd(ind),'ydata',yd(ind))
         set(ud.ruler.lines(5),'visible','on')
         if strcmp(ud.ruler.type,'track') | strcmp(ud.ruler.type,'slope')
         % snap rulers to valleys:
             ud = setrul(fig,ud,ud.ruler.value.x1,1);
             ud = setrul(fig,ud,ud.ruler.value.x2,2);
             set(fig,'userdata',ud)
         end

     else
         % button out
         if isempty(ud.focusline)
             return
         end
         xd = get(ud.focusline,'xdata');
         if isempty(xd)
             return
         end
         set(ud.ruler.lines(5),'visible','off')
        
         if btnstate(fig,'peaksgroup',1) 
             if  strcmp(ud.ruler.type,'track') | strcmp(ud.ruler.type,'slope')
                % snap rulers to peaks:
                ud = setrul(fig,ud,ud.ruler.value.x1,1);
                ud = setrul(fig,ud,ud.ruler.value.x2,2);
                set(fig,'userdata',ud)
             end
         end

      end
      
% --------------------------------------------------------------------
% ruler('updatePeaksgroup',fig)
%
case 'updatepeaksgroup'

    fig = varargin{2};
    ud = get(fig,'userdata');
    set(ud.ruler.lines(4),'visible','off')
    set(ud.ruler.lines(5),'visible','off')
    
    if btnstate(fig,'peaksgroup',1)
        ruler('peaks')
    end
    if btnstate(fig,'peaksgroup',2)
        ruler('valleys')  
    end
      
% --------------------------------------------------------------------
% ruler('rulerbutton', ruler_num)
%  button has been pressed to bring ruler to center of display
%  2nd input is 1 or 2 for which ruler.
%
case 'rulerbutton'
      fig = gcf;
      ud = get(fig,'userdata');
      
      ruler_num = varargin{2};

      if strcmp(ud.ruler.type,'horizontal')
          ylim = get(ud.mainaxes,'ylim');
          if strcmp(get(ud.mainaxes,'yscale'),'linear')
              val = mean(ylim);
          else
              val = sqrt(prod(ylim));
          end
      else
          xlim = get(ud.mainaxes,'xlim');
          if strcmp(get(ud.mainaxes,'xscale'),'linear')
              val = mean(xlim);
          else
              val = sqrt(prod(xlim));
          end
      end

      ud = setrul(fig,ud,val,ruler_num);
      set(fig,'userdata',ud)


% --------------------------------------------------------------------
% ruler('rulerbox',ruler_num)
%  Callbacks for the ruler edit boxes
%  2nd input is 1 or 2 for which ruler.
%
case 'rulerbox'
      fig = gcf;
      ud = get(fig,'userdata');
      
      ruler_num = varargin{2};

      % parse and evaluate string input
      the_box = ud.ruler.hand.boxes(ruler_num);

      if isempty(ud.focusline)
          set(the_box,'string','-')
          return
      end

      str = get(the_box,'string');

      [val,err] = validarg(str,[-Inf Inf],[1 1],'ruler value');
      if err
         val = get(the_box,'userdata');
         set(the_box,'string',num2str(val));
         return 
      else
         set(the_box,'userdata',val);
      end

      % make sure input is in the valid range 
      if strcmp(ud.ruler.type,'horizontal')
          ylim = get(ud.mainaxes,'ylim');
          val = inbounds(val,ylim);
      else
          xlim = get(ud.mainaxes,'xlim');
          val = inbounds(val,xlim);
      end

      % now set the ruler values
      ud = setrul(fig,ud,val,ruler_num,0,1);

      set(fig,'userdata',ud)

% --------------------------------------------------------------------
% ruler('inbounds',fig,XorYlim,plotIndex)
% Adjust ruler positions according to new axes limits as the axes
% scaling, range, or units are changed.  Enforces that ruler values stay
% within the new axes limits.
%
% Inputs:
%   fig - figure handle of the client
%   XorYlim - a string indicating which limits to adjust, X or Y limits
%             (the choices are: 'xlim' or 'ylim')
%   plotIndex - index into the list of 6 possible subplots
% Outputs:
%   None
%
case 'inbounds'
    fig = varargin{2};
    ud = get(fig,'userdata');
    plotIndex = varargin{4};
    XorYlim = varargin{3};
    
    switch XorYlim
    case 'xlim'
        XorYscale = 'xscale';
        lim = ud.limits(plotIndex).xlim;
        rulerval1 = ud.ruler.value.x1;
        rulerval2 = ud.ruler.value.x2;
    case 'ylim'
        XorYscale = 'yscale';        
        lim = ud.limits(plotIndex).ylim;
        rulerval1 = ud.ruler.value.y1;
        rulerval2 = ud.ruler.value.y2;
    end
    
    if (strcmp(XorYlim,'ylim') & strcmp(ud.ruler.type,'horizontal')) | ...
            (strcmp(XorYlim,'xlim') & ~strcmp(ud.ruler.type,'horizontal'))
        lim1 = lim(1);
        lim2 = lim(2);
        if strcmp(get(ud.mainaxes,XorYscale),'linear')
            val = mean(lim);
        else
            val = sqrt(prod(lim));
        end
        if (rulerval1 <= lim1) | (rulerval1 > lim2)
            ud = setrul(fig,ud,val,1,0,0,plotIndex);
        end
        if (rulerval2 <= lim1) | (rulerval2 > lim2)
            ud = setrul(fig,ud,val,2,0,0,plotIndex);
        end
        set(fig,'userdata',ud)
    end
end

function pos_ruler_labels(fig,sz,type,h,xtra_ui)
% pos_ruler_labels   Position the ruler labels and edit boxes.
%   Inputs:
%      fig - figure containing the ruler
%      sz - sizes structure
%      type - string signifying the ruler mode
%      h - handles structure
%   Outputs:
%      None

    fp = get(fig,'position');   % in pixels already

    toolbar_ht = sz.ih;
    frame_top = fp(4)-(toolbar_ht+sz.ffs+sz.lh/2);
    top = frame_top-(sz.lh+xtra_ui*(sz.uh+sz.fus)+sz.fus+2*sz.rih);

    switch type

    case 'vertical'
        vpos = {
        h.x1label    top
        h.x2label    top - 1*(sz.uh+sz.fus)
        h.dxlabel    top - 2*(sz.uh+sz.fus)
        };
        bi = [1 2];  % box indices
        texthandles = h.dxtext;
        ti = 3;      % text indices
    case 'horizontal'
        vpos = {
        h.y1label    top
        h.y2label    top - 1*(sz.uh+sz.fus)
        h.dylabel    top - 2*(sz.uh+sz.fus)
        };
        bi = [1 2];  % indices of boxes
        texthandles = h.dytext;
        ti = 3;      % text indices
    case 'track'
        vpos = {
        h.x1label    top
        h.y1label    top - 1*(sz.uh+sz.fus)
        h.x2label    top - 2*(sz.uh+sz.fus)
        h.y2label    top - 3*(sz.uh+sz.fus)
        h.dxlabel    top - 4*(sz.uh+sz.fus)
        h.dylabel    top - 5*(sz.uh+sz.fus)
        };
        bi = [1 3];  % indices of boxes
        texthandles = [h.y1text h.y2text h.dxtext h.dytext]';
        ti = [2 4 5 6];      % text indices
    case 'slope'
        vpos = {
        h.x1label    top
        h.y1label    top - 1*(sz.uh+sz.fus)
        h.x2label    top - 2*(sz.uh+sz.fus)
        h.y2label    top - 3*(sz.uh+sz.fus)
        h.dxlabel    top - 4*(sz.uh+sz.fus)
        h.dylabel    top - 5*(sz.uh+sz.fus)
        h.dydxlabel  top - 6*(sz.uh+sz.fus)
        };
        bi = [1 3];  % indices of boxes
        texthandles = [h.y1text h.y2text h.dxtext h.dytext h.dydxtext]';
        ti = [2 4 5 6 7];      % text indices
    end

    [boxpos,labelpos] = panelpos([vpos{:,1}],sz.ffs+sz.fus,...
       sz.rw-(sz.ffs+sz.fus), sz.uh, sz.lbs, cat(1,vpos{:,2}));

    for i=1:length(boxpos)  % add x-offset to uicontrols
        boxpos{i}=boxpos{i}+[fp(3)-sz.rw -2 0 4];
    end
    for i=1:length(labelpos)  % add x-offset to uicontrols
        labelpos{i}=labelpos{i}+[fp(3)-sz.rw 0 0 0];
    end
    box_tags = {'rulerbox1' 'rulerbox2'}';
    set(h.boxes(1:2),{'position'},boxpos(bi))
    set(texthandles,{'position'},boxpos(ti))
    set([vpos{:,1}],{'position'},labelpos)


function showhide_ruler_labels(fig,type,h)
%showhide_ruler_labels    Set visible property of ruler's labels and text
%   Inputs:
%      fig - figure which contains the ruler
%      type - string which contains either 'vertical' 'horizontal'
%            'track' or 'slope'
%          h - handle structure
%   Outputs:
%      None

    switch type
    case 'vertical'
        vis =   [h.x1label h.x2label h.dxlabel ...
                 h.dxtext ]';
        invis = [h.y1label h.y2label h.dylabel h.dydxlabel ...
                 h.y1text h.y2text h.dytext h.dydxtext]';
    case 'horizontal'
        vis =   [h.y1label h.y2label h.dylabel ...
                 h.dytext ]';
        invis = [h.x1label h.x2label h.dxlabel h.dydxlabel ...
                 h.y1text h.y2text h.dxtext h.dydxtext]';
    case 'track'
        vis =   [h.x1label h.y1label h.x2label h.y2label h.dxlabel h.dylabel ...
                 h.y1text h.y2text h.dxtext h.dytext]';
        invis = [h.dydxlabel h.dydxtext]';
    case 'slope'
        vis =   [h.x1label h.y1label h.x2label h.y2label h.dxlabel h.dylabel ...
                 h.dydxlabel h.y1text h.y2text h.dxtext h.dytext h.dydxtext]';
        invis = [];
    end
    set(vis,'visible','on')
    set(invis,'visible','off')


function id = ruler_btnid(type)
%ruler_btnid - Button number for ruler button group type string
%    type        id
% ------------  -----
% 'vertical'      1
% 'horizontal'    2
% 'track'         3
% 'slope'         4
%  otherwise     -1

switch type
case 'vertical'
    id = 1;
case 'horizontal'
    id = 2;
case 'track'
    id = 3;
case 'slope'
    id = 4;
otherwise
    id = -1;
end

function [ind,peaks] = findpeaks(y)
% FINDPEAKS  Find peaks in real vector.
%  ind = findpeaks(y) finds the indices (ind) which are
%  local maxima in the sequence y.  
%
%  [ind,peaks] = findpeaks(y) returns the value of the peaks at 
%  these locations, i.e. peaks=y(ind);

y = y(:)';

switch length(y)
case 0
    ind = [];
case 1
    ind = 1;
otherwise
    dy = diff(y);
    not_plateau_ind = find(dy~=0);
    ind = find( ([dy(not_plateau_ind) 0]<0) & ([0 dy(not_plateau_ind)]>0) );
    ind = not_plateau_ind(ind);
    if y(1)>y(2)
        ind = [1 ind];
    end
    if y(end-1)<y(end)
        ind = [ind length(y)];
    end
end

if nargout > 1
    peaks = y(ind);
end

function [boxpos,labelpos] = panelpos(labelhandles,left,right,uh,lbs,vpos)
%PANELPOS  Finds positions of labels and edit boxes.
%   [boxpos,labelpos]=panelpos(labelhandles,left,right,uh,lbs,vpos)
%   Purpose: This function maximizes the horizontal spacing of the edit
%      boxes in a list, taking the text extent of the labels into account.
%      Assumes labels are right justified.
%
%       left                    right
%         |                       |
%         |        lbs            I
%         v       |<-->|          v
% vpos(1)->   [label 1]    [edit box 1]     | uh
% vpos(2)->   [label 2]    [edit box 2]
% vpos(3)->   [label 3]    [edit box 3]
% vpos(4)->   [label 4]    [edit box 4]
%
%       inputs:
%     labelhandles - length N vector of axes text objects or 
%                        text uicontrols (all the same type).
%     left - left most pixel position (s

⌨️ 快捷键说明

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