Home > General-Functions > Graphics > ginput_10.m

ginput_10

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.  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.

   Examples:
       [x,y] = ginput;

       [x,y] = ginput(5);

       [x, y, button] = ginput(1);

   See also GTEXT, WAITFORBUTTONPRESS.

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

Generated on Sat 21-Jul-2018 20:56:10 by m2html © 2005