zinput

PURPOSE ^

ZINPUT - Graphical input from mouse with zoom

SYNOPSIS ^

function [out_regs] = zinput(arg1)

DESCRIPTION ^

 ZINPUT - Graphical input from mouse with zoom
   
   [OUT_REGS] = ZINPUT(N) gets N points or regions from the
   current axes and returns the X- and Y-ranges in a length Nx4
   matrix OUT_REGS. 
   The cursor can be positioned using a mouse. Data points are entered by
   pressing the right mouse button, the region of the current axis
   are selected with the middle button, left button is for
   zooming,  single cklick zooms in, click-and drag zooms to
   region (and doubble-click should zoom out - feature pending).
   Any key on the keyboard except carriage return zooms out to the
   orignal axis except carriage return, which terminates the input
   before N points are entered.
   
   [OUT_REGS] = ZINPUT gathers an unlimited number of points until the
   return key is pressed.
   
   Usage:
      [out_regs] = zinput(arg1)

   See also GINPUT

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function  [out_regs] = zinput(arg1)
0002 % ZINPUT - Graphical input from mouse with zoom
0003 %
0004 %   [OUT_REGS] = ZINPUT(N) gets N points or regions from the
0005 %   current axes and returns the X- and Y-ranges in a length Nx4
0006 %   matrix OUT_REGS.
0007 %   The cursor can be positioned using a mouse. Data points are entered by
0008 %   pressing the right mouse button, the region of the current axis
0009 %   are selected with the middle button, left button is for
0010 %   zooming,  single cklick zooms in, click-and drag zooms to
0011 %   region (and doubble-click should zoom out - feature pending).
0012 %   Any key on the keyboard except carriage return zooms out to the
0013 %   orignal axis except carriage return, which terminates the input
0014 %   before N points are entered.
0015 %
0016 %   [OUT_REGS] = ZINPUT gathers an unlimited number of points until the
0017 %   return key is pressed.
0018 %
0019 %   Usage:
0020 %      [out_regs] = zinput(arg1)
0021 %
0022 %   See also GINPUT
0023 
0024 % Copyright Bjorn Gustavsson 20050314
0025 
0026 ax0 = axis;
0027 out_regs = [];
0028 c = computer;
0029 if ~strcmp(c(1:2),'PC') 
0030   tp = get(0,'TerminalProtocol');
0031 else
0032   tp = 'micro';
0033 end
0034 
0035 if ~strcmp(tp,'none') & ~strcmp(tp,'x') & ~strcmp(tp,'micro') & 0,
0036   % I dont know about this so better make short-cut and blindly try
0037   % what works for X in all environments - sorry about that.
0038 else
0039   
0040   fig = gcf;
0041   figure(gcf);
0042   
0043   if nargin == 0
0044     how_many = inf;
0045     b = [];
0046   else
0047     how_many = arg1;
0048     b = [];
0049     if  isstr(how_many) ...
0050           | size(how_many,1) ~= 1 | size(how_many,2) ~= 1 ...
0051           | ~(fix(how_many) == how_many) ...
0052           | how_many < 0
0053       error('Requires a positive integer.');
0054     end
0055     if how_many == 0
0056       ptr_fig = 0;
0057       while(ptr_fig ~= fig)
0058         ptr_fig = get(0,'PointerWindow');
0059       end
0060       scrn_pt = get(0,'PointerLocation');
0061       loc = get(fig,'Position');
0062       pt = [scrn_pt(1) - loc(1), scrn_pt(2) - loc(2)];
0063       out1 = pt(1); y = pt(2);
0064     elseif how_many < 0
0065       error('Argument must be a positive integer.');
0066     end
0067   end
0068   
0069   % Remove figure button functions
0070   state = uisuspend(fig);
0071   pointer = get(gcf,'pointer');
0072   set(gcf,'pointer','fullcrosshair');
0073   
0074   fig_units = get(fig,'units');
0075   char = 0;
0076   while  size(out_regs,1) < how_many
0077     % Use no-side effect WAITFORBUTTONPRESS
0078     waserr = 0;
0079     try
0080       keydown = wfbp;
0081     catch
0082       waserr = 1;
0083     end
0084     if(waserr == 1)
0085       if(ishandle(fig))
0086         set(fig,'units',fig_units);
0087         uirestore(state);
0088         error('Interrupted');
0089       else
0090         error('Interrupted by figure deletion');
0091       end
0092     end
0093     
0094     ptr_fig = get(0,'CurrentFigure');
0095     if(ptr_fig == fig)
0096       if keydown
0097         axis(ax0);
0098         char = get(fig, 'CurrentCharacter');
0099       else
0100         char = get(fig, 'CurrentCharacter');
0101         button = abs(get(fig, 'CurrentCharacter'));
0102         
0103         pnt = get(gcf,'currentpoint');
0104         xy1 = get(gca,'currentpoint');
0105         rbbox([pnt 0 0],pnt)
0106         xy2 = get(gca,'currentpoint');
0107         selection_type = get(gcf,'selectiontype');
0108         
0109         if all(xy1==xy2)
0110           ax1 = axis;
0111           
0112           dx = abs(ax1(2)-ax1(1));
0113           dy = abs(ax1(4)-ax1(3));
0114           
0115           xmin = max(ax1(1),xy1(1)-dx/4);
0116           xmax = min(ax1(2),xy1(1)+dx/4);
0117           ymin = max(ax1(3),xy1(1,2)-dy/4);
0118           ymax = min(ax1(4),xy1(1,2)+dy/4);
0119           zoom2ax = [xmin xmax ymin ymax];
0120         else
0121           %%% zoom to selected rectangle
0122           zoom2ax = [sort([xy1(1,1) xy2(1,1)]) sort([xy1(1,2) xy2(1,2)])];
0123         end
0124         
0125         switch selection_type
0126          case 'normal'
0127           axis(zoom2ax)
0128           reg = [];
0129          case 'alt'
0130           reg = axis;
0131           axis(ax0)
0132          case 'extend'
0133           reg = xy1(1,[1 1 2 2]);
0134           axis(ax0)
0135          otherwise %%% open (doubleclick in linux)
0136           axis(ax0);
0137           reg = [];
0138         end
0139       end
0140       pt = get(gca, 'CurrentPoint');
0141       
0142       if (char == 'r')
0143         rmi = size(out_regs,1);
0144         if rmi > 0
0145           out_regs(rmi,:) = [];
0146         end
0147         set(fig, 'CurrentCharacter','q')
0148         char = 'q';
0149         reg = [];
0150       end
0151       if(char == 13) % & how_many ~= 0)
0152                      % if the return key was pressed, char will == 13,
0153                      % and that's our signal to break out of here whether
0154                      % or not we have collected all the requested data
0155                      % points.
0156                      % If this was an early breakout, don't include
0157                      % the <Return> key info in the return arrays.
0158                      % We will no longer count it if it's the last input.
0159         break;
0160       end
0161       
0162       if ~isempty(reg)
0163         out_regs = [out_regs;reg];
0164       end
0165     end
0166     %[size(out_regs,1), how_many, size(out_regs,1) < how_many]
0167   end
0168   
0169   uirestore(state);
0170   set(gcf,'pointer','arrow');
0171   set(fig,'units',fig_units);
0172   set(fig, 'CurrentCharacter','q');
0173   
0174 end
0175 
0176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0177 function key = wfbp
0178 %WFBP   Replacement for WAITFORBUTTONPRESS that has no side effects.
0179 
0180 fig = gcf;
0181 current_char = [];
0182 
0183 % Now wait for that buttonpress, and check for error conditions
0184 waserr = 0;
0185 try
0186   h=findall(fig,'type','uimenu','accel','C');   % Disabling ^C for edit menu so the only ^C is for
0187   set(h,'accel','');                            % interrupting the function.
0188   keydown = waitforbuttonpress;
0189   current_char = double(get(fig,'CurrentCharacter')); % Capturing the character.
0190   if~isempty(current_char) & (keydown == 1)           % If the character was generated by the
0191     if(current_char == 3)                       % current keypress AND is ^C, set 'waserr'to 1
0192       waserr = 1;                             % so that it errors out.
0193     end
0194   end
0195   
0196   set(h,'accel','C');                                 % Set back the accelerator for edit menu.
0197 catch
0198   waserr = 1;
0199 end
0200 drawnow;
0201 if(waserr == 1)
0202   set(h,'accel','C');                                % Set back the accelerator if it errored out.
0203   error('Interrupted');
0204 end
0205 
0206 selection_type = get(gcf,'selectiontype');
0207 if strcmp(selection_type,'open')
0208   axis(ax0)
0209 end
0210 if nargout>0, key = keydown; end
0211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Generated on Sun 29-Mar-2009 20:22:55 by m2html © 2003