Home > utilities > ginput2.m

ginput2

PURPOSE ^

GINPUT2 Graphical input from mouse with zoom, pan, plot and scaling.

SYNOPSIS ^

function [X,Y,BUTTON,SCALEMAT] = ginput2(varargin)

DESCRIPTION ^

GINPUT2   Graphical input from mouse with zoom, pan, plot and scaling.
   
   SYNTAX:
                        XY = ginput2;
                        XY = ginput2(DoScale);          true or false
                        XY = ginput2(...,PlotOpt);      '.r' for example
                        XY = ginput2(...,'KeepZoom');   vs. 'UnZoom'
                        XY = ginput2(N,...);
                        XY = ginput2(...);
                     [X,Y] = ginput2(...);
              [X,Y,BUTTON] = ginput2(...);
     [X,Y,BUTTON,SCALEMAT] = ginput2(...);

   INPUT:
     DoScale    - Single logical specifying whether the IMAGE should be
                  interactively scaled (georeferenced), or it can be the
                  2x4 SCALEMAT matrix for automatically scaling.
                  DEFAULT: false (do not scales/georeferences)
     PlotOpt    - String and/or parameter/value pairs specifying the drawn
                  points optional inputs (see PLOT for details). 
                  DEFAULT: 'none' (do not plot any point)
     'KeepZoom' - When finishing selection by default the zoom is
                  restored. By using this option this feature is ignored.
                  DEFAULT: 'UnZoom' (restores original axis limits)
     N          - Number of points to be selected. One of 0,1,2,...,Inf
                  DEFAULT: Inf (selects until ENTER or ESCAPE is pressed)

   OUTPUT:
     XY        - [X(:) Y(:)] axis coordinate(s).
     X         - X-coordinate(s).
     Y         - Y-coordinate(s).
     BUTTON    - Last pressed button.
     SCALEMAT  - 2x4 matrix specifying the coordinates of two different
                 points (1) and (2) in the Image coordinates (pixels) and
                 the User coordinates (data):
                                                Point 1     Point 2
                   Image coord (pixels):     [ (I1x,I1y)   (I2x,I2y) 
                   User  coord (data)  :       (U1x,U1y)   (U2x,U2y) ]
                 to be use for scaling/georeferencing.

   DESCRIPTION:
     This program uses MATLAB's GINPUT function to get the coordinates
     of a mouse-selected point in the current figure (see GINPUT for
     details), but with five major improvements:
                  1. ZOOMING  (left click)
                  2. PANNING  (dragging mouse)
                  3. DELETING (last selected point)
                  4. PLOTING  (temporarily the selected points)
                  5. SCALING or GEOREFERENCE IMAGES.
     The differences are:
      a) Obviously, the SCALEOPT and PlotOpt optional arguments.
      b) When click is made outside the axes, it is ignored.
      c) When LEFT-click, ZOOM-IN is performed right into the selected
         point (PANNING).
      d) When RIGHT-click, the point is selected (normal).
      e) When DOUBLE-click, ZOOM-OUT is done.
      f) When MIDDLE-click, ZOOM-RESET is done (see ZOOM for details).
      g) When dragging while pressed left-click PAN is done (until the
         button is released).
      h) When pressed any KEY follows the next rules: 
          A) If ENTER is pressed, the selection is terminated. If no point
             was already selected, the outputs are empty's.
          B) If BACKSPACE key is pressed, the last selected point is
             deleted and the selection continues.
          C) If SPACEBAR the mouse current position or NANs coordinates
             are saved, depending whether the mouse was inside or outside
             any of the current figure axes, respectively. In this latter
             case, the selection is NOT counted as one of the N points.
             Besides, when drawing the color is changed. Then, the outputs
             may not be of length N.

   NOTE:
     * Optional inputs use its DEFAULT value when not given or [].
     * Optional outputs may or not be called.
     * String inputs may be shortened, as long as they are unambiguous.
       Case is ignored.
     * The function can be used for interactively digitalize/vectorize
       RASTER images with:
       >> ginput(true)
     * The function can be used only as a georeference function with 
       >> ginput2(0,true)
     * The scale/georeference only works when the current axes has an
       IMAGE type children (see Image for details). 
     * The x and y data from axes and image are changed when scale/
       georeference is used.
     * The drawn points are deleted from the graphics once the selection
       is finished. 
     * The priority of the inputs are: N, then SCALEOPT and finally
       PlotOpt. If the first (integer) is missing, the next is taken into
       account (logical or 2x4 matrix) and so on.

   EXAMPLE:
     % Selects until ENTER is pressed:
         xy = ginput2;
     % Selects 5 points:
         [x,y] = ginput2(5);
     % Gets pressed button:
         [x,y,button] = ginput2(1);
     % Scales image and select 4 points temporarily coloring them in
       black. Besides to not ZOOM OUT at the end:
         imagesc(peaks(40))
         [x,y,button,scalemat] = ginput2(4,true,'k*','KeepZoom');
         hold on, plot(x,y,'or'), hold off

   SEE ALSO:
     GINPUT, PLOT.


   ---
   MFILE:   ginput2.m
   VERSION: 3.1 (Nov 12, 2009) (<a href="matlab:web('http://www.mathworks.com/matlabcentral/fileexchange/authors/11258')">download</a>)
   MATLAB:  7.7.0.471 (R2008b)
   AUTHOR:  Carlos Adrian Vargas Aguilera (MEXICO)
   CONTACT: nubeobscura@hotmail.com

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function  [X,Y,BUTTON,SCALEMAT] = ginput2(varargin)
0002 %GINPUT2   Graphical input from mouse with zoom, pan, plot and scaling.
0003 %
0004 %   SYNTAX:
0005 %                        XY = ginput2;
0006 %                        XY = ginput2(DoScale);          true or false
0007 %                        XY = ginput2(...,PlotOpt);      '.r' for example
0008 %                        XY = ginput2(...,'KeepZoom');   vs. 'UnZoom'
0009 %                        XY = ginput2(N,...);
0010 %                        XY = ginput2(...);
0011 %                     [X,Y] = ginput2(...);
0012 %              [X,Y,BUTTON] = ginput2(...);
0013 %     [X,Y,BUTTON,SCALEMAT] = ginput2(...);
0014 %
0015 %   INPUT:
0016 %     DoScale    - Single logical specifying whether the IMAGE should be
0017 %                  interactively scaled (georeferenced), or it can be the
0018 %                  2x4 SCALEMAT matrix for automatically scaling.
0019 %                  DEFAULT: false (do not scales/georeferences)
0020 %     PlotOpt    - String and/or parameter/value pairs specifying the drawn
0021 %                  points optional inputs (see PLOT for details).
0022 %                  DEFAULT: 'none' (do not plot any point)
0023 %     'KeepZoom' - When finishing selection by default the zoom is
0024 %                  restored. By using this option this feature is ignored.
0025 %                  DEFAULT: 'UnZoom' (restores original axis limits)
0026 %     N          - Number of points to be selected. One of 0,1,2,...,Inf
0027 %                  DEFAULT: Inf (selects until ENTER or ESCAPE is pressed)
0028 %
0029 %   OUTPUT:
0030 %     XY        - [X(:) Y(:)] axis coordinate(s).
0031 %     X         - X-coordinate(s).
0032 %     Y         - Y-coordinate(s).
0033 %     BUTTON    - Last pressed button.
0034 %     SCALEMAT  - 2x4 matrix specifying the coordinates of two different
0035 %                 points (1) and (2) in the Image coordinates (pixels) and
0036 %                 the User coordinates (data):
0037 %                                                Point 1     Point 2
0038 %                   Image coord (pixels):     [ (I1x,I1y)   (I2x,I2y)
0039 %                   User  coord (data)  :       (U1x,U1y)   (U2x,U2y) ]
0040 %                 to be use for scaling/georeferencing.
0041 %
0042 %   DESCRIPTION:
0043 %     This program uses MATLAB's GINPUT function to get the coordinates
0044 %     of a mouse-selected point in the current figure (see GINPUT for
0045 %     details), but with five major improvements:
0046 %                  1. ZOOMING  (left click)
0047 %                  2. PANNING  (dragging mouse)
0048 %                  3. DELETING (last selected point)
0049 %                  4. PLOTING  (temporarily the selected points)
0050 %                  5. SCALING or GEOREFERENCE IMAGES.
0051 %     The differences are:
0052 %      a) Obviously, the SCALEOPT and PlotOpt optional arguments.
0053 %      b) When click is made outside the axes, it is ignored.
0054 %      c) When LEFT-click, ZOOM-IN is performed right into the selected
0055 %         point (PANNING).
0056 %      d) When RIGHT-click, the point is selected (normal).
0057 %      e) When DOUBLE-click, ZOOM-OUT is done.
0058 %      f) When MIDDLE-click, ZOOM-RESET is done (see ZOOM for details).
0059 %      g) When dragging while pressed left-click PAN is done (until the
0060 %         button is released).
0061 %      h) When pressed any KEY follows the next rules:
0062 %          A) If ENTER is pressed, the selection is terminated. If no point
0063 %             was already selected, the outputs are empty's.
0064 %          B) If BACKSPACE key is pressed, the last selected point is
0065 %             deleted and the selection continues.
0066 %          C) If SPACEBAR the mouse current position or NANs coordinates
0067 %             are saved, depending whether the mouse was inside or outside
0068 %             any of the current figure axes, respectively. In this latter
0069 %             case, the selection is NOT counted as one of the N points.
0070 %             Besides, when drawing the color is changed. Then, the outputs
0071 %             may not be of length N.
0072 %
0073 %   NOTE:
0074 %     * Optional inputs use its DEFAULT value when not given or [].
0075 %     * Optional outputs may or not be called.
0076 %     * String inputs may be shortened, as long as they are unambiguous.
0077 %       Case is ignored.
0078 %     * The function can be used for interactively digitalize/vectorize
0079 %       RASTER images with:
0080 %       >> ginput(true)
0081 %     * The function can be used only as a georeference function with
0082 %       >> ginput2(0,true)
0083 %     * The scale/georeference only works when the current axes has an
0084 %       IMAGE type children (see Image for details).
0085 %     * The x and y data from axes and image are changed when scale/
0086 %       georeference is used.
0087 %     * The drawn points are deleted from the graphics once the selection
0088 %       is finished.
0089 %     * The priority of the inputs are: N, then SCALEOPT and finally
0090 %       PlotOpt. If the first (integer) is missing, the next is taken into
0091 %       account (logical or 2x4 matrix) and so on.
0092 %
0093 %   EXAMPLE:
0094 %     % Selects until ENTER is pressed:
0095 %         xy = ginput2;
0096 %     % Selects 5 points:
0097 %         [x,y] = ginput2(5);
0098 %     % Gets pressed button:
0099 %         [x,y,button] = ginput2(1);
0100 %     % Scales image and select 4 points temporarily coloring them in
0101 %       black. Besides to not ZOOM OUT at the end:
0102 %         imagesc(peaks(40))
0103 %         [x,y,button,scalemat] = ginput2(4,true,'k*','KeepZoom');
0104 %         hold on, plot(x,y,'or'), hold off
0105 %
0106 %   SEE ALSO:
0107 %     GINPUT, PLOT.
0108 %
0109 %
0110 %   ---
0111 %   MFILE:   ginput2.m
0112 %   VERSION: 3.1 (Nov 12, 2009) (<a href="matlab:web('http://www.mathworks.com/matlabcentral/fileexchange/authors/11258')">download</a>)
0113 %   MATLAB:  7.7.0.471 (R2008b)
0114 %   AUTHOR:  Carlos Adrian Vargas Aguilera (MEXICO)
0115 %   CONTACT: nubeobscura@hotmail.com
0116 
0117 %   REVISIONS:
0118 %   1.0      Released. (Jul 09, 2008)
0119 %   2.0      Changed default YESERROR value and fixed a bug there. Changed
0120 %            behavior when N==1. Fixed bug with zoom out. Changed default
0121 %            selection keys. Changed default selection click mouse: from
0122 %            left one to the right one. (Jun 08, 2009)
0123 %   2.1      Fixed bugs related with points deletion. Added new 'KeepZoom'
0124 %            feature. (Aug 20, 2009)
0125 %   3.0      Now it PANs when dragging. Updated help. (Nov 05, 2009)
0126 %   3.1      Now returns when N==1 and pressed not predefined KEYS or one
0127 %            of DELECTION or RETURN buttons. (Nov 12, 2009)
0128 
0129 %   DISCLAIMER:
0130 %   ginput2.m is provided "as is" without warranty of any kind, under the
0131 %   revised BSD license.
0132 
0133 %   Copyright (c) 2008-2009 Carlos Adrian Vargas Aguilera
0134 
0135 
0136 % INPUTS CHECK-IN
0137 % -------------------------------------------------------------------------
0138 
0139 % PARAMETERS
0140 % Defaults:
0141 X        = [];
0142 Y        = [];
0143 BUTTON   = [];
0144 SCALEMAT = [];
0145 N        = Inf;
0146 DoScale  = false;
0147 PlotOpt  = {'none'};
0148 UnZoom   = 'UnZoom';
0149 % Constants KEYs (on my personal keyboard):
0150 DOUBLECLICK    =   0;
0151 LEFTCLICK      =   1;
0152 MIDDLECLICK    =   2;
0153 RIGHTCLICK     =   3;
0154 BACKSPACE      =   8;
0155 ESCAPE         =  27;
0156 LEFTARROW      =  28;
0157 RIGHTARROW     =  29;
0158 UPARROW        =  30;
0159 DOWNARROW      =  31;
0160 SPACEBAR       =  32;
0161 DELETE         = 127;
0162 ASCII          = [ ...
0163                     33:64  ...  UP-KEYS
0164                     65:90  ...  UP-LETTERS
0165                     91:96  ... LOW-KEYS
0166                     97:122 ... LOW-LETTERS
0167                    123:126 ... LOW-KEY
0168                    161:255 ...     FOREING
0169                    ];
0170 % Functionality:
0171 % NOTE: I left all this KEYs because the user may use this special case for
0172 % other special purposes outside this function.
0173 % % First version default:
0174 % % SELECTS   = [LEFTCLICK ASCII ESCAPE LEFTARROW RIGHTARROW ...
0175 % %              UPARROW DOWNARROW SPACEBAR DELETE];
0176 % % ZOOMIN    = RIGHTCLICK;
0177 SELECTS   = [RIGHTCLICK SPACEBAR]; % Selection  buttons
0178 DELETES   = BACKSPACE;             % Deletion   buttons
0179 FINISHES  = [];                    % Finishes   buttons
0180 ZOOMIN    = LEFTCLICK;             % ZOOM(2)    buttons
0181 ZOOMRESET = MIDDLECLICK;           % ZOOM RESET buttons
0182 ZOOMOUT   = DOUBLECLICK;           % ZOOM OUT   buttons
0183 % Other parameters
0184 secpause  = 0.3;    % Seconds to wait for double-click response.
0185 YESERROR  = false;  % If there is an error with GINPUT, it tells to display
0186                     % an ERROR or a WARNING message.
0187                     
0188 % Checks number of inputs:
0189 if nargout>4
0190  error('CVARGAS:ginput2:tooManyOutputs',...
0191   'At most 4 outputs are allowed.')
0192 end
0193 
0194 % Checks N:
0195 if ~isempty(varargin) && ~isempty(varargin{1}) && ...
0196   isfloat(varargin{1}) 
0197  N           = round(abs(varargin{1}(1))); % Forced unique, positive
0198  varargin(1) = [];                         % integer.
0199 end
0200 
0201 % Checks DoScale:
0202 if ~isempty(varargin) && ~isempty(varargin{1}) && ...
0203    ((islogical(varargin{1})) || (ndims(varargin{1})==2 && ...
0204     all(size(varargin{1})==[2 4])))
0205  DoScale     = varargin{1};
0206  varargin(1) = [];
0207 end
0208 
0209 % Checks UnZoom:
0210 if ~isempty(varargin) 
0211  if ~isempty(varargin{1}) && ischar(varargin{1})
0212   if     strncmpi(varargin(1),'UnZoom'  ,max(length(varargin{1}),2))
0213    UnZoom = 'UnZoom';
0214    varargin(1) = [];
0215   elseif strncmpi(varargin(1),'KeepZoom',max(length(varargin{1}),2))
0216    UnZoom = 'KeepZoom';
0217    varargin(1) = [];
0218   end
0219  elseif (length(varargin)>1) && ~isempty(varargin{end}) && ...
0220      ischar(varargin{end})
0221   if     strncmpi(varargin(end),'UnZoom'  ,max(length(varargin{1}),2))
0222    UnZoom = 'UnZoom';
0223    varargin(end) = [];
0224   elseif strncmpi(varargin(end),'KeepZoom',max(length(varargin{1}),2))
0225    UnZoom = 'KeepZoom';
0226    varargin(end) = [];
0227   end
0228  end
0229 end
0230 
0231 % Checks PlotOpt:
0232 if ~isempty(varargin) && ~isempty(varargin{1})
0233  PlotOpt = varargin;
0234 end
0235 clear varargin
0236 
0237 % Checks DoScale:
0238 if ~islogical(DoScale)
0239  SCALEMAT = DoScale;
0240  DoScale = true;
0241 end
0242 
0243 % SCALES/GEOREFERENCE?:
0244 if DoScale
0245  method = 'linear';
0246  extrap = 'extrap';
0247  ha     = gca;
0248  hi     = findobj(get(ha,'Children'),'Type','image');
0249  axes(ha)
0250  if ~isempty(hi)
0251   hi    = hi(1);
0252   xlim  = get(ha,'XLim');
0253   ylim  = get(ha,'YLim');
0254   zlim  = get(ha,'ZLim');
0255   z     = repmat(max(zlim),1,5);
0256   xdata = get(hi,'XData');
0257   ydata = get(hi,'YData');
0258   if isempty(SCALEMAT) % interactively
0259    I1x = round(min(xdata)); I2x = round(max(xdata));
0260    I1y = round(min(ydata)); I2y = round(max(ydata));
0261    % Default (equal):
0262    U1x = I1x; U2x = I2x;
0263    U1y = I1y; U2y = I2y;
0264    hgeo     = [];
0265    dlgTitle = 'Georeference image';
0266    lineNo   = 1;
0267   
0268    while true
0269     % Selects first corner:
0270     theans = ...
0271           questdlg('Select the first corner (1 of 2):',dlgTitle,'OK','OK');
0272     if ~strcmp(theans,'OK'), return, end
0273     pause(secpause)
0274     
0275     [I1x,I1y] = ginput2(1,false,'none','UnZoom');
0276     I1x       = round(I1x);
0277     I1y       = round(I1y);
0278     if ~ishandle(ha), return, end
0279     if (ha==gca) && ~isempty(I1x) && ~isnan(I1x)
0280      axis(ha,[xlim ylim])
0281      hgeo(1) = line([xlim NaN I1x I1x],[I1y I1y NaN ylim],z,'color','m');
0282      prompt  = {'X-coordinate at 1st corner:',...
0283                 'Y-coordinate at 1st corner:'};
0284      def     = {int2str(I1x),int2str(I1y)};
0285      answer  = inputdlg(prompt,dlgTitle,lineNo,def);
0286      answer  = str2num(char(answer{:}));
0287      break
0288     end
0289    end
0290    axes(ha)
0291    
0292    % Checks inputs:
0293    if ~isempty(answer) && isfloat(answer) && (length(answer)==2) && ...
0294                                                       all(isfinite(answer))
0295     U1x = answer(1); U1y = answer(2);
0296     secondcorner = true;
0297    else
0298     secondcorner = false;
0299     warning('CVARGAS:ginput2:incorrectGeoreference',...
0300             'Ignored incorrect georeference corners.')
0301    end
0302   
0303    while secondcorner
0304     % Selects second corner:
0305     theans = ...
0306          questdlg('Select the second corner (2 of 2):',dlgTitle,'OK','OK');
0307     if ~strcmp(theans,'OK'), return, end
0308     pause(secpause)
0309    
0310     [I2x,I2y] = ginput2(1,false,'none','UnZoom');
0311     I2x       = round(I2x);
0312     I2y       = round(I2y);
0313     if ~ishandle(ha), return, end
0314     if (ha==gca) && ~isempty(I2x) && ~isnan(I2x) && ...
0315       (I2x~=I1x) && (I2y~=I1y)
0316      axis(ha,[xlim ylim])
0317      hgeo(2) = line([xlim NaN I2x I2x],[I2y I2y NaN ylim],z,'color','c');
0318      prompt  = {'X-coordinate at 2nd corner:',...
0319                 'Y-coordinate at 2nd corner:'};
0320      def     = {int2str(I2x),int2str(I2y)};
0321      answer  = inputdlg(prompt,dlgTitle,lineNo,def);
0322      answer  = str2num(char(answer{:}));
0323      break
0324     end
0325    end
0326    axes(ha)
0327    
0328    % Checks inputs:
0329    if secondcorner && ~isempty(answer) && isfloat(answer) && ...
0330                          (length(answer)==2) && all(isfinite(answer))
0331     U2x = answer(1); U2y = answer(2);
0332    else
0333     warning('CVARGAS:ginput2:incorrectGeoreference',...
0334             'Ignored incorrect georeference corners.')
0335    end
0336   
0337    % Deletes corner's lines:
0338    if any(ishandle(hgeo))
0339     delete(hgeo(ishandle(hgeo)))
0340    end
0341    
0342    % Scale matrix:
0343     SCALEMAT = [I1x I1y I2x I2y; U1x U1y U2x U2y];
0344   else
0345    % Continue
0346   end
0347  else
0348   warning('CVARGAS:ginput2:noImageFound',...
0349    'No image found in the current axes to georeference.')
0350  end
0351  
0352  % OK, set the scaling then:
0353  if ~isempty(SCALEMAT)
0354   xdata = interp1(SCALEMAT(1,[1 3]),SCALEMAT(2,[1 3]),xdata,method,extrap);
0355   ydata = interp1(SCALEMAT(1,[2 4]),SCALEMAT(2,[2 4]),ydata,method,extrap);
0356   xlim2 = interp1(SCALEMAT(1,[1 3]),SCALEMAT(2,[1 3]),xlim ,method,extrap);
0357   ylim2 = interp1(SCALEMAT(1,[2 4]),SCALEMAT(2,[2 4]),ylim ,method,extrap);
0358   set(hi,'XData',xdata);
0359   set(hi,'YData',ydata);  
0360   set(ha,'XLim' ,sort(xlim2,'ascend'));
0361   set(ha,'YLim' ,sort(ylim2,'ascend'));
0362   % Reverses axis directions:
0363   if diff(xlim)*diff(xlim2)<1
0364    if strcmp(get(ha,'XDir'),'normal')
0365     set(ha,'XDir','reverse')
0366    else
0367     set(ha,'XDir','normal')
0368    end
0369   end
0370   if diff(ylim)*diff(ylim2)<1
0371    if strcmp(get(ha,'YDir'),'normal')
0372     set(ha,'YDir','reverse')
0373    else
0374     set(ha,'YDir','normal')
0375    end
0376   end
0377  end
0378  axis(ha,'normal')
0379  
0380 end
0381 
0382 % DRAWS?:
0383 if strcmpi(PlotOpt{1},'none')
0384  yesdraw = false;
0385 else
0386  yesdraw = true;
0387 end
0388 % Optional parameters:
0389 if yesdraw
0390  hpoints  = [];
0391  % Check for linestyle color:
0392  yescolor     = true;
0393  Nplotopt     = length(PlotOpt);
0394  yeslinestyle = rem(Nplotopt,2);
0395  if yeslinestyle % Given LineStyle
0396   for k = 1:length(PlotOpt{1})
0397    switch lower(PlotOpt{1}(k))
0398     case 'y', yescolor = false; break
0399     case 'm', yescolor = false; break
0400     case 'c', yescolor = false; break
0401     case 'r', yescolor = false; break
0402     case 'g', yescolor = false; break
0403     case 'b', yescolor = false; break
0404     case 'w', yescolor = false; break
0405     case 'k', yescolor = false; break
0406     otherwise, % no color specified
0407    end
0408   end
0409  end
0410  if ~yescolor && (Nplotopt*yeslinestyle~=1)
0411   for k = yeslinestyle+1:2:Nplotopt    % Given 'Color'
0412    if strncmpi(PlotOpt{k},'co',2), yescolor = false; break, end
0413   end
0414  end
0415  if yescolor
0416   contnan  = 1;
0417   colors   = get(gca,'ColorOrder');
0418   ncolors  = size(colors,1);
0419   color    = colors(1,:);
0420  end
0421 end
0422 
0423 
0424 % -------------------------------------------------------------------------
0425 % MAIN
0426 % -------------------------------------------------------------------------
0427 
0428 cont       = 0;
0429 alim.ha    = [];
0430 alim.la    = {};
0431 undoPtrFig = [];
0432 
0433 while cont<N     % Principal loop
0434  
0435  % GINPUT:
0436  try
0437   [x,y,button] = ginput(1);
0438  catch % Changed for compatibility.
0439   % GINPUT error:
0440   if YESERROR
0441      error('CVARGAS:ginput2:executionError',lasterr)
0442   else
0443    warning('CVARGAS:ginput2:executionError',lasterr)
0444    if nargout<2  % Fixed BUG 29 SEP, 2008
0445     X = [X Y]; 
0446    end
0447    return
0448   end
0449  end
0450  
0451  % Axes clicked:
0452  ha = gca;
0453  
0454  % Gets limits:
0455  if ~any(alim.ha==ha)
0456   alim.ha(end+1) = ha;
0457   alim.la{end+1} = axis;
0458  end
0459  
0460  % Sets zoom:
0461  zlim = getappdata(ha,'zoom_zoomOrigAxesLimits');
0462  if isempty(zlim) % Fixed BUG, SEP 2008
0463   zoom reset
0464   zlim = getappdata(ha,'zoom_zoomOrigAxesLimits');
0465  end
0466  
0467  % Checks if DOUBLE clicks:
0468  pause(secpause) % Gives time for response
0469  if strcmp(get(gcf,'selectiontype'),'open')
0470   button = DOUBLECLICK;
0471  end
0472 
0473  % Checks if ENTER or FINISHES button:
0474  if isempty(button) || ismember(button,FINISHES)
0475   % Finishes selection:
0476   if (N==1) && isempty(X) % New feature v3.1
0477    BUTTON = button;
0478   end
0479   break
0480  end
0481  
0482  % Checks if DELETION button:
0483  if ismember(button,DELETES)
0484   if ~isempty(X)
0485    inan = isnan(X(end));
0486    if yesdraw
0487     if ~inan
0488      % Deletes last drawn point:
0489      if ~isempty(hpoints) && ishandle(hpoints(end)) % Fixed bug Aug 2009
0490       delete(hpoints(end)), hpoints(end) = [];
0491      end
0492     elseif yescolor
0493      % Change color as necessary:
0494      contnan = contnan-1;
0495      color   = colors(mod(contnan-1,ncolors)+1,:);
0496     end
0497    end
0498    % Deletes the last selected point:
0499    X(end)      = [];
0500    Y(end)      = [];
0501    BUTTON(end) = [];
0502    % Checks if the last point was NaN:
0503    if ~inan
0504     cont = cont-1;
0505    end
0506   elseif N==1
0507    % Finishes selection: New feature v3.1
0508    BUTTON = button;
0509    break
0510   end
0511   continue
0512  end
0513  
0514  % Checks if ZOOM OUT button:
0515  if ismember(button,ZOOMOUT)
0516   undoPtrFig = gcf;
0517   setptr(undoPtrFig,'glassminus')
0518   zoom out
0519   continue
0520  end
0521  
0522  % Checks if ZOOM RESET button:
0523  if ismember(button,ZOOMRESET)
0524   zoom reset
0525   continue
0526  end
0527  
0528  % Checks if the mouse was inside an axes of the current figure;
0529  lim     = axis;
0530  outside = x<lim(1) || x>lim(2) || y<lim(3) || y>lim(4);
0531  
0532  % Checks if ZOOM IN with PAN:
0533  if ismember(button,ZOOMIN) && ~outside
0534   % Dragging rectangle:
0535   undoPtrFig = gcf;
0536   setptr(undoPtrFig,'closedhand')
0537   rbbox
0538   ydrag = get(gca,'CurrentPoint');
0539   xdrag = ydrag(1,1)-x;
0540   ydrag = ydrag(1,2)-y;
0541   % Do the PANNING:
0542   if  any(abs([xdrag ydrag])>eps*1000000)
0543    % Only PAN (dragging):
0544    lim(1:4) = lim(1:4) - [xdrag xdrag ydrag ydrag]; 
0545    axis(lim)
0546   else
0547    % PAN (centers the last point) and ZOOM in:
0548    setptr(undoPtrFig,'glassplus')
0549    lim = [x+diff(lim(1:2))/2*[-1 1] y+diff(lim(3:4))/2*[-1 1]]; 
0550    axis(lim)
0551    zoom(2)
0552   end
0553   continue
0554  end
0555  
0556  % Checks if SELECTS button:
0557  if ismember(button,SELECTS)
0558   
0559   % Sets NaNs if outside the axes:
0560   if outside 
0561    if ~isnumeric(N) 
0562     % Change color:
0563     if yesdraw && yescolor 
0564      contnan = contnan+1;
0565      color   = colors(mod(contnan-1,ncolors)+1,:);
0566     end
0567     % Adds NaNs but the point counters do not take it into account:
0568     X      = [X;      NaN];
0569     Y      = [Y;      NaN];
0570     BUTTON = [BUTTON; button];
0571    else
0572     % Ignores the point
0573    end
0574   else
0575    % Draws the result:
0576    if yesdraw
0577     % Search for last point:
0578     x0 = []; y0 = []; z0 = [];
0579     inan = isnan([NaN; X; NaN]);
0580     if ~inan(end-1)
0581      inan        = find(inan);
0582      nlastpoints = inan(end)-inan(end-1)-1;
0583      npoints     = length(hpoints);
0584      range       = npoints-nlastpoints+1:npoints;
0585      hlastaxes   = get(hpoints(range),'Parent');
0586      if iscell(hlastaxes), hlastaxes  = cell2mat(hlastaxes); end
0587      [loc,loc] = ismember(ha,hlastaxes);
0588      if loc
0589       x0 = get(hpoints(range(loc)),'XData');
0590       y0 = get(hpoints(range(loc)),'YData');
0591       z0 = get(hpoints(range(loc)),'ZData');
0592      end
0593     end
0594     holdon = ishold;
0595     if ~holdon, hold on, end 
0596      h = plot([x0 x],[y0 y],PlotOpt{:});
0597      % Elevates the value:
0598      z = get(ha,'ZLim'); z = z(2);
0599      set(h,'Zdata',[z0 z])
0600      % Sets the color:
0601      if yescolor
0602       set(h,'Color',color)
0603      end
0604      hpoints = [hpoints; h];
0605     if ~holdon, hold off, end 
0606     % Restores limits:
0607     axis(lim)
0608    end
0609    
0610    % Centers the selected point if ZOOM-IN: 29 SEP,2008
0611    if all((lim~=zlim))
0612     lim = [x+diff(lim(1:2))/2*[-1 1] y+diff(lim(3:4))/2*[-1 1]];
0613     axis(lim)
0614    end
0615    
0616    % Saves the result:
0617    X      = [X;      x];
0618    Y      = [Y;      y];
0619    BUTTON = [BUTTON; button];
0620    cont   = cont+1;
0621   end
0622   continue
0623  end
0624 
0625  % Checks if any other button pressed inside the axes:
0626  if ~outside
0627   X      = [X;      x];
0628   Y      = [Y;      y];
0629   BUTTON = [BUTTON; button];
0630   cont   = cont+1;
0631  else
0632   if N==1 % New feature v3.1
0633    BUTTON = button;
0634    break
0635   end
0636   % ignores the selection
0637  end
0638  
0639 end
0640 
0641 % Returns pointer.
0642 if ~isempty(undoPtrFig) && ishandle(undoPtrFig)
0643  setptr(undoPtrFig,'arrow')
0644 end
0645 
0646 % Deletes drawn points if still exist:
0647 if yesdraw && any(ishandle(hpoints))
0648  delete(hpoints(ishandle(hpoints)))
0649 end
0650 
0651 % Returns original limits.
0652 if ~strcmp(UnZoom,'KeepZoom') && ~isempty(alim.ha)
0653  alim.ha(~ishandle(alim.ha)) = [];
0654  for k = 1:length(alim.ha)
0655   temp = axis(alim.ha(k));
0656   if ~all(temp(1:4)==alim.la{k}(1:4))
0657    axis(alim.ha(k),alim.la{k})
0658   end
0659  end
0660 end
0661 
0662 
0663 % OUTPUTS CHECK-OUT
0664 % -------------------------------------------------------------------------
0665 
0666 if nargout<2
0667  X = [X Y]; 
0668 end
0669 
0670 
0671 % [EOF]   ginput2.m

Generated on Wed 20-Feb-2019 16:06:01 by m2html © 2005