


UISelect - Interactively select polygon zone in an existing plot.
USAGE
[X,Y,p] = UISelect
X,Y polygon coordinates
p handle for polygon object in figure
SEE
See also UIInPolygon

0001 function [X,Y,polygon] = UISelect 0002 0003 %UISelect - Interactively select polygon zone in an existing plot. 0004 % 0005 % USAGE 0006 % 0007 % [X,Y,p] = UISelect 0008 % 0009 % X,Y polygon coordinates 0010 % p handle for polygon object in figure 0011 % 0012 % SEE 0013 % 0014 % See also UIInPolygon 0015 % 0016 0017 % Copyright (C) 2009-2011 by Michaƫl Zugaro 0018 % 0019 % This program is free software; you can redistribute it and/or modify 0020 % it under the terms of the GNU General Public License as published by 0021 % the Free Software Foundation; either version 3 of the License, or 0022 % (at your option) any later version. 0023 0024 hold on; 0025 X = []; 0026 Y = []; 0027 polygon = []; 0028 last = false; 0029 0030 while ~last, 0031 % Get next mouse click 0032 [x,y,button] = ginput(1); 0033 switch button, 0034 case 1, 0035 % Left button adds this point 0036 X(end+1,1) = x; 0037 Y(end+1,1) = y; 0038 case 2, 0039 % Middle button closes the selection 0040 last = true; 0041 if ~isempty(X), 0042 X(end+1,1) = X(1); 0043 Y(end+1,1) = Y(1); 0044 end 0045 case 3, 0046 % Right button cancels last point 0047 if ~isempty(X), 0048 X(end) = []; 0049 Y(end) = []; 0050 end 0051 end 0052 % Update existing polygon 0053 if isempty(polygon), 0054 polygon = plot(X,Y); 0055 else 0056 warning('off','MATLAB:hg:line:XDataAndYDataLengthsMustBeEqual'); 0057 set(polygon,'XData',X); 0058 set(polygon,'YData',Y); 0059 warning('on','MATLAB:hg:line:XDataAndYDataLengthsMustBeEqual'); 0060 end 0061 end 0062 0063 hold off;