ginputquick

PURPOSE ^

GINPUT - Graphical input from mouse.

SYNOPSIS ^

function [out1,out2,out3] = ginput(arg1)

DESCRIPTION ^

GINPUT - Graphical input from mouse.

   [X,Y] = GINPUT(N) gets N points from the current axes and returns 
   the X- and Y-coordinates in length N vectors X and Y.  The cursor
   can be positioned using a mouse (or by using the Arrow Keys on some 
   systems).  Data points are entered by pressing a mouse button
   or any key on the keyboard except carriage return, which terminates
   the input before N points are entered.

   [X,Y] = GINPUT gathers an unlimited number of points until the
   return key is pressed.
 
   [X,Y,BUTTON] = GINPUT(N) returns a third result, BUTTON, that 
   contains a vector of integers specifying which mouse button was
   used (1,2,3 from left) or ASCII numbers if a key on the keyboard
   was used.

   Usage:
      [out1,out2,out3] = ginput(arg1)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [out1,out2,out3] = ginput(arg1)
0002 %GINPUT - Graphical input from mouse.
0003 %
0004 %   [X,Y] = GINPUT(N) gets N points from the current axes and returns
0005 %   the X- and Y-coordinates in length N vectors X and Y.  The cursor
0006 %   can be positioned using a mouse (or by using the Arrow Keys on some
0007 %   systems).  Data points are entered by pressing a mouse button
0008 %   or any key on the keyboard except carriage return, which terminates
0009 %   the input before N points are entered.
0010 %
0011 %   [X,Y] = GINPUT gathers an unlimited number of points until the
0012 %   return key is pressed.
0013 %
0014 %   [X,Y,BUTTON] = GINPUT(N) returns a third result, BUTTON, that
0015 %   contains a vector of integers specifying which mouse button was
0016 %   used (1,2,3 from left) or ASCII numbers if a key on the keyboard
0017 %   was used.
0018 %
0019 %   Usage:
0020 %      [out1,out2,out3] = ginput(arg1)
0021 
0022 %   Copyright 1984-2005 The MathWorks, Inc.
0023 %   $Revision: 1.2 $  $Date: 2009/03/27 00:44:53 $
0024 
0025 out1 = []; out2 = []; out3 = []; y = [];
0026 c = computer;
0027 if ~strcmp(c(1:2),'PC') 
0028    tp = get(0,'TerminalProtocol');
0029 else
0030    tp = 'micro';
0031 end
0032 
0033 if ~strcmp(tp,'none') && ~strcmp(tp,'x') && ~strcmp(tp,'micro'),
0034    if nargout == 1,
0035       if nargin == 1,
0036          out1 = trmginput(arg1);
0037       else
0038          out1 = trmginput;
0039       end
0040    elseif nargout == 2 || nargout == 0,
0041       if nargin == 1,
0042          [out1,out2] = trmginput(arg1);
0043       else
0044          [out1,out2] = trmginput;
0045       end
0046       if  nargout == 0
0047          out1 = [ out1 out2 ];
0048       end
0049    elseif nargout == 3,
0050       if nargin == 1,
0051          [out1,out2,out3] = trmginput(arg1);
0052       else
0053          [out1,out2,out3] = trmginput;
0054       end
0055    end
0056 else
0057    
0058    fig = gcf;
0059    figure(gcf);
0060    
0061    if nargin == 0
0062       how_many = -1;
0063       b = [];
0064    else
0065       how_many = arg1;
0066       b = [];
0067       if  ischar(how_many) ...
0068             || size(how_many,1) ~= 1 || size(how_many,2) ~= 1 ...
0069             || ~(fix(how_many) == how_many) ...
0070             || how_many < 0
0071          error('MATLAB:ginput:NeedPositiveInt', 'Requires a positive integer.')
0072       end
0073       if how_many == 0
0074          ptr_fig = 0;
0075          while(ptr_fig ~= fig)
0076             ptr_fig = get(0,'PointerWindow');
0077          end
0078          scrn_pt = get(0,'PointerLocation');
0079          loc = get(fig,'Position');
0080          pt = [scrn_pt(1) - loc(1), scrn_pt(2) - loc(2)];
0081          out1 = pt(1); y = pt(2);
0082       elseif how_many < 0
0083          error('MATLAB:ginput:InvalidArgument', 'Argument must be a positive integer.')
0084       end
0085    end
0086    
0087    % Suspend figure functions
0088    state = uisuspend(fig);
0089    
0090    toolbar = findobj(allchild(fig),'flat','Type','uitoolbar');
0091    if ~isempty(toolbar)
0092         ptButtons = [uigettool(toolbar,'Plottools.PlottoolsOff'), ...
0093                      uigettool(toolbar,'Plottools.PlottoolsOn')];
0094         ptState = get (ptButtons,'Enable');
0095         set (ptButtons,'Enable','off');
0096    end
0097 
0098    set(fig,'pointer','fullcrosshair');
0099    fig_units = get(fig,'units');
0100    char = 0;
0101 
0102    % We need to pump the event queue on unix
0103    % before calling WAITFORBUTTONPRESS
0104    drawnow
0105    
0106    while how_many ~= 0
0107       % Use no-side effect WAITFORBUTTONPRESS
0108       waserr = 0;
0109       try
0110     keydown = wfbp;
0111       catch
0112     waserr = 1;
0113       end
0114       if(waserr == 1)
0115          if(ishandle(fig))
0116             set(fig,'units',fig_units);
0117         uirestore(state);
0118             error('MATLAB:ginput:Interrupted', 'Interrupted');
0119          else
0120             error('MATLAB:ginput:FigureDeletionPause', 'Interrupted by figure deletion');
0121          end
0122       end
0123       
0124       ptr_fig = get(0,'CurrentFigure');
0125       if(ptr_fig == fig)
0126          if keydown
0127             char = get(fig, 'CurrentCharacter');
0128             button = abs(get(fig, 'CurrentCharacter'));
0129             scrn_pt = get(0, 'PointerLocation');
0130             set(fig,'units','pixels')
0131             loc = get(fig, 'Position');
0132             pt = [scrn_pt(1) - loc(1), scrn_pt(2) - loc(2)];
0133             set(fig,'CurrentPoint',pt);
0134          else
0135             button = get(fig, 'SelectionType');
0136             if strcmp(button,'open') 
0137                button = 1;
0138             elseif strcmp(button,'normal') 
0139                button = 1;
0140             elseif strcmp(button,'extend')
0141                button = 2;
0142             elseif strcmp(button,'alt') 
0143                button = 3;
0144             else
0145                error('MATLAB:ginput:InvalidSelection', 'Invalid mouse selection.')
0146             end
0147          end
0148          pt = get(gca, 'CurrentPoint');
0149          
0150          how_many = how_many - 1;
0151          
0152          if(char == 13) % & how_many ~= 0)
0153             % if the return key was pressed, char will == 13,
0154             % and that's our signal to break out of here whether
0155             % or not we have collected all the requested data
0156             % points.
0157             % If this was an early breakout, don't include
0158             % the <Return> key info in the return arrays.
0159             % We will no longer count it if it's the last input.
0160             break;
0161          end
0162 
0163          out1 = [out1;pt(1,1)];
0164          y = [y;pt(1,2)];
0165          b = [b;button];
0166       end
0167    end
0168    
0169    uirestore(state);
0170    if ~isempty(toolbar) && ~isempty(ptButtons)
0171         set (ptButtons(1),'Enable',ptState{1});
0172         set (ptButtons(2),'Enable',ptState{2});
0173    end
0174    set(fig,'units',fig_units);
0175    
0176    if nargout > 1
0177       out2 = y;
0178       if nargout > 2
0179          out3 = b;
0180       end
0181    else
0182       out1 = [out1 y];
0183    end
0184 
0185    line(out1,y);
0186    line([out1(length(out1)) out1(1)],[y(length(y)) y(1)]);
0187    
0188 end
0189 
0190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0191 function key = wfbp
0192 %WFBP   Replacement for WAITFORBUTTONPRESS that has no side effects.
0193 
0194 fig = gcf;
0195 current_char = [];
0196 
0197 % Now wait for that buttonpress, and check for error conditions
0198 waserr = 0;
0199 try
0200   h=findall(fig,'type','uimenu','accel','C');   % Disabling ^C for edit menu so the only ^C is for
0201   set(h,'accel','');                            % interrupting the function.
0202   keydown = waitforbuttonpress;
0203   current_char = double(get(fig,'CurrentCharacter')); % Capturing the character.
0204   if~isempty(current_char) && (keydown == 1)           % If the character was generated by the
0205       if(current_char == 3)                       % current keypress AND is ^C, set 'waserr'to 1
0206           waserr = 1;                             % so that it errors out.
0207       end
0208   end
0209   
0210   set(h,'accel','C');                                 % Set back the accelerator for edit menu.
0211 catch
0212   waserr = 1;
0213 end
0214 drawnow;
0215 if(waserr == 1)
0216    set(h,'accel','C');                                % Set back the accelerator if it errored out.
0217    error('MATLAB:ginput:Interrupted', 'Interrupted');
0218 end
0219 
0220 if nargout>0, key = keydown; end
0221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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