-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselectdata.m
More file actions
1282 lines (1088 loc) · 35.9 KB
/
selectdata.m
File metadata and controls
1282 lines (1088 loc) · 35.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function [pointslist,xselect,yselect] = selectdata(varargin)
% selectdata: graphical selection of data points on a plot using the mouse
% usage: pointslist = selectdata % uses all default options
% usage: pointslist = selectdata(prop1,val1,prop2,val2,...)
%
% SELECTDATA allows the user to select data points on a given plot
% using the mouse, in a variety of modes. 'Lasso' mode allows the
% selection by a user directed lasso around the points. 'Brush' mode
% selects points as you brush over them with the mouse. 'Rect' mode
% draws a rectangle, selecting any points inside the rectangle.
% 'Closest' mode looks for a aingle mouse click, finding the closest
% point to the mouse.
%
% Returned is a list of the point indexes selected, additionally you
% can specify that the points be deleted from the plot.
%
% Arguments: (input)
% The input arguments to SELECTDATA are all property/value pairs.
% (See PARSE_PV_PAIRS for more details.)
% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=9082&objectType=FILE
%
% Property names and character values can be shortened as long as
% the shortening is unambiguous. Capitalization is ignored.
%
% Valid Properties: 'Action', 'Axes', 'BrushSize', 'Identify',
% 'Ignore' , 'Pointer', 'SelectionMode'
%
% 'Action' - {'list', 'delete'}
% 'delete' causes the selected points to be deleted from
% the figure after their selection is final.
%
% DEFAULT VALUE: 'List'
%
% 'Axes' - denotes the axes from which to select points
%
% DEFAULT VALUE: gca
%
% 'BrushShape' - {'rect', 'circle'}
%
% DEFAULT VALUE: 'circle'
%
% Sets the shape of the brush to be used. Both brush
% shapes are relative to the figure axes, so a nominally
% "circular" brush is actually elliptical if the axis
% units/lengths are unequal.
%
% Only used when SelectionMode is 'brush'.
%
% 'BrushSize' - Only used when SelectionMode is 'brush'.
%
% DEFAULT VALUE: 0.05
%
% The default value will specify a rectangular brush
% that has dimensions of 5% of the current axes. Note
% that on a set of square axes, the brush will always
% look square, even if the axes have very different
% units.
%
% 0 < brushsize <= 0.25
%
% 'Identify' - {'on', 'off'}
% Causes the selected points to be temporarily over-plotted
% with a new filled red marker ('o') as they are selected.
%
% Points selected with a lasso, or rect may be deselected
% as the tool is modified. Brush selections are cumulative.
%
% DEFAULT VALUE: 'on'
%
% 'Ignore' - a data handle, or []
%
% A list of data handles to be ignored. This allows you to
% use selectdata on only some sets of points, while others
% in the same figure are ignored. This is a useful option
% when you may have plotted some data points but also a
% curve fit through your data. You can then cause the plotted
% curve to be ignored by selectdata.
%
% DEFAULT VALUE: []
%
% 'Label' - {'off', 'on'}
%
% Causes text labels with their (x,y) coordinates to appear
% next to each point selected.
%
% Beware that selecting large numbers of points and creating
% and displaying the label for them can be time consuming.
% This option is a great one for single point selection,
% but I have seen system-related problems when rapidly
% selecting & deselecting large numbers of points with the
% rect tool.
%
% DEFAULT VALUE: 'off'
%
% 'Pointer' - {'crosshair' | 'fullcrosshair' | 'arrow' | 'ibeam' |
% 'watch' | 'topl' | 'topr' | 'botl' | 'botr' | 'left' |
% 'top' | 'right' | 'bottom' | 'circle' | 'cross' | 'fleur' |
% 'hand' }
%
% Changes the cursor pointer while selection is active.
% After selection terminates, the figure pointer is
% restored to its old setting.
%
% DEFAULT VALUE: 'crosshair'
%
% 'Return' - {'selected' | 'unselected' }
%
% Selection of data points, perhaps if used to indicate
% outliers in data, would normally return that set of
% points selected. But some users may prefer to see the
% list of points returned to be those points which were
% NOT selected.
%
% DEFAULT VALUE: 'selected'
%
% 'SelectionMode' - {'Lasso', 'Brush', 'Rect', 'Closest'}
%
% DEFAULT VALUE: 'Lasso'
%
% If 'Brush' is used, then the brush will be a rectangular
% brush, with a size as defined by the 'BrushSize' property.
% The brush will be centered at the current mouse coordinates.
% Brush size will be a fraction of the axis limits for the
% current axes. Click inside the axes, then drag the "brush"
% around. Any points the brush crosses over will be selected.
%
% If 'Lasso' is chosen, then click inside the axes to define
% one end of the lasso, then drag with the mouse still down,
% causing the mouse to define a general curvilinear region.
% The polygon will close automatically when the mouse button
% is released. A point is "inside" the lasso if inpolygon
% identifies it as so. BEWARE of convoluted lassos that
% intersect themselves.
%
% If 'Rect' is chosen, then click inside the axes to define
% one corner of the rect, dragging to specify the opposite
% corner, just as rbbox would do.
%
% If 'Closest' was chosen, then a single mouse click in the
% figure window is used, then that point which is closest in
% in Euclidean distance (in window units) is chosen. You
% can move the cursor around (don't release the mouse until
% you are done) and the currently selected point will be
% highlighted.
%
% 'Verify' - { 'off' | 'on' }
%
% If set to 'on', this causes a dialog box to pop up after
% selection. The user can then acccept the selection, redo
% it, or cancel out, causing no points to be selected.
%
% Note, if cancel is chosen from the dialog, and 'return'
% was specify to return those points 'unselected', then ALL
% the points will actually be returned.
%
% DEFAULT VALUE: 'off'
%
%
% Note: other properties are available for use, but I've chosen to
% leave them semi-hidden because they don't seem terribly useful to
% most users. These properties allow you to specify the colors of the
% selection tool itself, the colors of the selected point markers,
% the transparency of the selection tool, the marker itself, etc.
% Default values for these parameters are:
%
% FlagMarker = 'o'
% FlagColor = 'r'
% Fill = 'on'
% FillColor = 'y'
% FillEdgeColor = 'b'
% FillTrans = 0.5
% MaxBrush = 0.25
% RemoveTool = 'on'
% RemoveFlagged = 'on'
% RemoveLabels = 'on'
%
% Further documentation on these parameters can be found by editting
% selectdata.m itself.
%
%
% Arguments: (output)
% pointslist - list of points that were selected. If only one
% dataset was found, then points list is a simple vector.
% If multiple sets of points were found in the axes, then
% points list will be a cell array.
%
% NOTE: Each set of points is peeled off the stach in the
% matlab stores it.
%
% xselect - array (or cell array) containing the x coordinates of
% those points identified in the selection
%
% yselect - array (or cell array) containing the y coordinates of
% those points identified in the selection
%
%
% Example:
% Plot a set of points, then select some with the mouse
% using a rectangular brush, return the indices of those
% points selected. (Note: user interaction on the plot
% will be necessary.)
%
% x = 0:.1:1;
% y = x.^2;
% plot(x,y,'o')
% pl = selectdata('selectionmode','brush');
%
% Example:
% Select a single point with the mouse, then delete that
% selected point from the plot.
%
% pl = selectdata('selectionmode','closest','action','delete');
%
% Example:
% Select some points using a rect(rbbox) tool, also return
% the (x,y) coordinates from multiple curves plotted. Use
% shortened versions of the properties and values.
%
% plot(rand(5,2),rand(5,2),'o')
% [pl,xs,ys] = selectdata('sel','r');
%
% Example:
% Plot a curve and some data points on one plot, select
% some points from the data plotted, but ignore the
% smooth curve, even if the lasso passes over it.
% x = 0:.01:1;
% y = exp(x);
% ynoisy = y + randn(size(y))/2;
% h1 = plot(x,y,'-');
% hold on
% h2 = plot(x,ynoisy,'o');
% [pl,xs,ys] = selectdata('sel','lasso','ignore',h1);
%
% See also:
%
%
% Author: John D'Errico
% E-mail: woodchips@rochester.rr.com
% Release: 3.0
% Release date: 2/19/07
% defaults for the parameters
params.Axes = gca;
params.SelectionMode = 'lasso';
params.Action = 'list';
params.BrushShape = 'circle';
params.BrushSize = .05;
params.Identify = 'on';
params.Ignore = [];
params.Pointer = 'cross';
params.Return = 'selected';
params.Verify = 'off';
params.Label = 'off';
% Undocumented options, also unchecked for validity.
% These parameters control the marker used to identify
% those points which are currently selected.
% FlagMarker must be a valid plot marker, FlagColor
% must be a valid color spec. The default values are...
params.FlagMarker = 'o';
params.FlagColor = 'r';
% More (unchecked) options that are yours to fiddle with
% (or not.) These control the fill color to be applied
% to the interior of the lasso, rect, and brush. Also
% controlled are the degree of transparency to be applied.
params.Fill = 'on';
params.FillColor = 'y';
params.FillEdgeColor = 'b';
params.FillTrans = 0.5; % must be in the interval [0,1]
% The maximum relative brushsize allowed is also
% controlled here, just in case I ever wanted to allow
% the brush to be a bit larger.
params.MaxBrush = 0.25;
% After selection has been accomplished, the flagged points
% and the selection tool itself are normally deleted. But
% in some circumstances it may be useful to not delete
% those objects. 'off' will cause these objects to remain.
params.RemoveTool = 'on';
params.RemoveFlagged = 'on';
params.RemoveLabels = 'on';
% save this to restore later
axissize = axis;
% process any Property/value pairs.
if nargin>0
params = parse_pv_pairs(params,varargin);
end
% check the supplied parameters for validity
params = check_params(params);
% bring the focus to the figure containing the
% designated axes
fighandle = get(params.Axes,'parent');
figure(fighandle)
% get the current figure pointer, so we can
% restore it later on
oldpointer = get(fighandle,'Pointer');
% extract xdata and ydata from the specified axes
% get the children of the axes
hc = get(params.Axes,'children');
% are any of the data handles to be ignored?
if ~isempty(params.Ignore)
hc = setdiff(hc,params.Ignore);
end
% strip out xdata and ydata
xdata = get(hc,'xdata');
ydata = get(hc,'ydata');
% if we must highlight the points as they are
% selected, then for efficiency we need to know
% how many we may expect.
flaghandle = [];
if ~iscell(xdata)
xdata = xdata(:);
ydata = ydata(:);
% total number of data points
npoints = length(xdata);
else
for i = 1:length(xdata)
xdata{i} = xdata{i}(:);
ydata{i} = ydata{i}(:);
end
% total number of data points
npoints = cellfun('length',xdata);
end
% for textlabels if desired
texthandles = [];
% set up a while loop in case we need to verify
% satisfaction
selectionflag = true;
while selectionflag
% and the total number currently selected
nsel = 0;
% what SelectionMode was specified
switch params.SelectionMode
case 'closest'
% Find the single closest point (in Euclidean distance)
% to the mouse click
% set the figure pointer
if ~isempty(params.Pointer)
set(fighandle,'Pointer',params.Pointer)
end
% mouse click?
waitforbuttonpress;
% Button Motion and Button Up
set(fighandle,'WindowButtonMotionFcn',@CPmotion);
set(fighandle,'WindowButtonUpFcn',@selectdone);
% dx, dy to scale the distance
dx = (axissize(2) - axissize(1));
dy = (axissize(4) - axissize(3));
% current closest point is
cc = get(gca,'CurrentPoint');
xy = cc(1,1:2);
% what point was closest?
[pointslist,xselect,yselect] = closestpoint(xy,xdata,ydata,dx,dy);
nsel = 1;
% identify any points?
if strcmp(params.Identify,'on')
flagpoints
end
% label them?
if strcmp(params.Label,'on')
maketextlabels
end
% selecthandle is not needed for this mode op operation
selecthandle = [];
% wait until the mouse button was released
uiwait
% ....
% resume.
% all we need to do here is restore the figure pointer
% if we changed it before
if ~isempty(params.Pointer)
set(fighandle,'Pointer',oldpointer)
end
case 'rect'
% Selection rect as a polygon
% mouse click?
waitforbuttonpress;
% set the figure pointer
if ~isempty(params.Pointer)
set(fighandle,'Pointer',params.Pointer)
end
% button down detected
cc = get(gca,'CurrentPoint');
rectxy1 = cc(1,1:2);
rectxy2 = rectxy1 + eps(rectxy1);
% make a polygon of the box, initially of nil area
xv = [rectxy1(1), rectxy2(1), rectxy2(1), rectxy1(1), rectxy1(1)];
yv = [rectxy1(2), rectxy1(2), rectxy2(2), rectxy2(2), rectxy1(2)];
% no points should been selected
[pointslist,xselect,yselect,nsel] = testpoly(xv,yv,xdata,ydata);
% and plot it, filled or not
hold on
if strcmp(params.Fill,'on')
% filled
selecthandle = fill(xv,yv,params.FillColor);
set(selecthandle,'facealpha',params.FillTrans, ...
'linestyle','--','edgecolor',params.FillEdgeColor)
else
% unfilled
selecthandle = plot(xv,yv,'r:');
end
% we can undo the hold now
hold off
% Button Motion and Button Up
set(fighandle,'WindowButtonMotionFcn',@rectmotion);
set(fighandle,'WindowButtonUpFcn',@selectdone);
% wait until the selection is done
uiwait
% ....
% resume.
% The rect already is a polygon, stored in (xv,yv)
case 'lasso'
% Selection lasso as a polygon
% mouse click?
waitforbuttonpress;
% set the figure pointer
if ~isempty(params.Pointer)
set(fighandle,'Pointer',params.Pointer)
end
% button down detected
cc = get(gca,'CurrentPoint');
xlasso = cc(1,1);
ylasso = cc(1,2);
% form the polygon
xv = xlasso;
yv = ylasso;
% and plot it, filled or not
hold on
if strcmp(params.Fill,'on')
% filled
selecthandle = fill(xv,yv,params.FillColor);
set(selecthandle,'facealpha',params.FillTrans, ...
'linestyle','--','edgecolor',params.FillEdgeColor)
else
% unfilled
selecthandle = plot(xv,yv,'r:');
end
% we can undo the hold now
hold off
% Button Motion and Button Up
set(fighandle,'WindowButtonMotionFcn',@lassomotion);
set(fighandle,'WindowButtonUpFcn',@selectdone);
% wait until the selection is done
uiwait
% ....
% resume.
% The lasso already is a polygon, stored in (xv,yv)
case 'brush'
% paint over the data, with a rectangular brush
% mouse click?
waitforbuttonpress;
% set the figure pointer
if ~isempty(params.Pointer)
set(fighandle,'Pointer',params.Pointer)
end
% button down detected
bc = get(gca,'CurrentPoint');
brushcenter = bc(1,1:2);
% dx, dy for the brush
bdx = params.BrushSize*(axissize(2) - axissize(1));
bdy = params.BrushSize*(axissize(4) - axissize(3));
if strcmpi(params.BrushShape,'rect')
% make the brush polygon as a fixed size rectangle
% that we can slide around
xv = brushcenter(1) + [-1, 1, 1, -1, -1]*bdx/2;
yv = brushcenter(2) + [-1, -1, 1, 1, -1]*bdy/2;
else
% a circle was specified
theta = linspace(0,2*pi,100);
xv = cos(theta)*bdx/2 + brushcenter(1);
yv = sin(theta)*bdy/2 + brushcenter(2);
end
% draw the initial brush polygon, filled or not
hold on
if strcmp(params.Fill,'on')
% filled
selecthandle = fill(xv,yv,params.FillColor);
set(selecthandle,'facealpha',params.FillTrans, ...
'linestyle','-','edgecolor',params.FillEdgeColor)
else
% unfilled
selecthandle = plot(xv,yv,'r:');
end
hold off
% have any points been selected?
[pointslist,xselect,yselect,nsel] = testpoly(xv,yv,xdata,ydata);
% identify any points?
if strcmp(params.Identify,'on') && (nsel>0)
flagpoints
end
% label them?
if strcmp(params.Label,'on') && (nsel>0)
maketextlabels
end
% Button Motion and Button Up
set(fighandle,'WindowButtonMotionFcn',@brushmotion);
set(fighandle,'WindowButtonUpFcn',@selectdone);
% wait until the selection is done
uiwait
% ....
% resume.
end
% verify?
if strcmpi(params.Verify,'on')
% pop up a dialog
ButtonName = questdlg( ...
'Are you satisfied with the points selected?','???', ...
'Yes','Redo Selection','Cancel Selection','Yes');
switch ButtonName
case 'Yes'
% we can drop through
selectionflag = false;
case 'Cancel Selection'
% drop out, with nothing selected
if ~iscell(xdata)
pointslist = [];
xselect = [];
yselect = [];
else
for i = 1:numel(xdata);
pointslist{i} = [];
xselect{i} = [];
yselect{i} = [];
end
end
% we can drop through
selectionflag = false;
case 'Redo Selection'
% or try again. The while loop will cycle
% until happy or canceled
end
else
% no verification was requested, so we want to
% terminate the while loop after only one pass through.
selectionflag = false;
end
end
% pointslist and xselect, yselect are already complete.
% Do we delete the selected points?
if strcmpi(params.Action,'delete')
if ~iscell(xdata)
% only one set, so xdata and ydata are not cell arrays
% Which points from the data fall in the selection polygon?
xdata(pointslist) = [];
ydata(pointslist) = [];
% drop those points from the plot
set(hc,'xdata',xdata,'ydata',ydata)
else
% it was a cell array, so there were multiple sets.
for i = 1:numel(xdata);
xdata{i}(pointslist{i}) = [];
ydata{i}(pointslist{i}) = [];
% drop those points from the plot
set(hc(i),'xdata',xdata{i},'ydata',ydata{i})
end
end
end
% was 'return' set to be the selected list or the unselected one?
% Do nothing if 'selected', we are already done.
if strcmpi(params.Return,'unselected')
if ~iscell(xdata)
% only one set, so xdata and ydata are not cell arrays
pointslist = setdiff((1:npoints)',pointslist);
xselect = xdata(pointslist);
yselect = ydata(pointslist);
else
% it was a cell array, so there were multiple sets.
for i = 1:numel(xdata);
pointslist{i} = setdiff((1:npoints(i))',pointslist{i});
xselect{i} = xdata{i}(pointslist{i});
yselect{i} = ydata{i}(pointslist{i});
end
end
end
% =====================================================
% begin nested functions
% =====================================================
function brushmotion(src,evnt) %#ok
% nested function for motion of the brush
% get the new mouse position
mousenew = get(params.Axes,'CurrentPoint');
mousenew = mousenew(1,1:2);
% make sure the axes are fixed
axis(axissize)
% how far did it move
brushoffset = mousenew - brushcenter;
brushcenter = mousenew;
xv = xv + brushoffset(1);
yv = yv + brushoffset(2);
% did we brush over any new points
[pl,xselect,yselect,nsel] = testpoly(xv,yv,xdata,ydata);
% for a brush, we need to append any selected points to
% the already selected list
if ~iscell(xdata)
% only one set, so xdata and ydata are not cell arrays
pointslist = union(pointslist,pl);
xselect = xdata(pointslist);
yselect = ydata(pointslist);
nsel = length(pointslist);
else
% it was a cell array, so there were multiple sets.
for j = 1:numel(pointslist);
pointslist{j} = union(pointslist{j},pl{j}); %#ok
pointslist{j} = pointslist{j}(:); %#ok
if ~isempty(pointslist{j})
xselect{j} = xdata{j}(pointslist{j});
yselect{j} = ydata{j}(pointslist{j});
end
end
% total of points selected
nsel = sum(cellfun('length',pointslist));
end
% identify any points?
if strcmp(params.Identify,'on')
flagpoints
end
% label them?
if strcmp(params.Label,'on')
maketextlabels
end
% replot the brush in its new position
set(selecthandle,'xdata',xv,'ydata',yv)
end
% =====================================================
% =====================================================
function CPmotion(src,evnt) %#ok
% nested function to select the closest point
% get the new mouse position
mousenew = get(params.Axes,'CurrentPoint');
xy = mousenew(1,1:2);
% make sure the axes stay fixed
axis(axissize)
% what point was closest?
[pointslist,xselect,yselect] = closestpoint(xy,xdata,ydata,dx,dy);
nsel = 1;
% identify any points?
if strcmp(params.Identify,'on')
flagpoints
end
% label them?
if strcmp(params.Label,'on')
maketextlabels
end
end
% =====================================================
% =====================================================
function rectmotion(src,evnt) %#ok
% nested function for expansion or contraction of the rect
% get the new mouse position
mousenew = get(params.Axes,'CurrentPoint');
rectxy2 = mousenew(1,1:2);
% make sure the axes are fixed
axis(axissize)
% update the rect polygon of the box, changing the second corner
xv = [rectxy1(1), rectxy2(1), rectxy2(1), rectxy1(1), rectxy1(1)];
yv = [rectxy1(2), rectxy1(2), rectxy2(2), rectxy2(2), rectxy1(2)];
% did we brush over any new points
[pointslist,xselect,yselect,nsel] = testpoly(xv,yv,xdata,ydata);
% identify any points?
if strcmp(params.Identify,'on')
flagpoints
end
% label them?
if strcmp(params.Label,'on')
maketextlabels
end
% replot the rect in its new position
set(selecthandle,'xdata',xv,'ydata',yv)
end
% =====================================================
% =====================================================
function lassomotion(src,evnt) %#ok
% nested function for expansion of the lasso
% get the new mouse position
mousenew = get(params.Axes,'CurrentPoint');
mousenew = mousenew(1,1:2);
% append the new point on the end of the last lasso
xlasso = [xlasso,mousenew(1,1)];
ylasso = [ylasso,mousenew(1,2)];
% and close it to form the polygon
xv = [xlasso,xlasso(1)];
yv = [ylasso,ylasso(1)];
% replot the newly extended lasso
set(selecthandle,'xdata',xv,'ydata',yv)
% did we enclose any new points?
[pointslist,xselect,yselect,nsel] = testpoly(xv,yv,xdata,ydata);
% identify any points?
if strcmp(params.Identify,'on')
flagpoints
end
% label them?
if strcmp(params.Label,'on')
maketextlabels
end
% make sure the axes are maintained in size
axis(axissize)
end
% =====================================================
% =====================================================
function selectdone(src,evnt) %#ok
% nested function for mouse up
% do we remove the selection tool?
if strcmpi(params.RemoveTool,'on')
% delete the selection object from the plot
delete(selecthandle)
selecthandle = [];
end
% do we remove the flagged points?
if strcmpi(params.RemoveFlagged,'on')
% also remove the flagged/plotted points
if ~isempty(flaghandle)
delete(flaghandle)
flaghandle = [];
end
end
% do we remove the flagged points?
if strcmpi(params.RemoveLabels,'on')
% also remove the labels
delete(texthandles)
texthandles = [];
end
% cancel the WindowButtonFcn's that we had set
set(fighandle,'WindowButtonMotionFcn',[]);
set(fighandle,'WindowButtonUpFcn',[]);
% restore the figure pointer to its original setting
if ~isempty(params.Pointer)
set(fighandle,'Pointer',oldpointer)
end
% and resume execution, back in the mainline
uiresume
end
% =====================================================
% =====================================================
function flagpoints
% nested function for flagging the selected points
% Are these the first points flagged? If so,
% we need to plot them and set the marker, etc.
if isempty(flaghandle) && (nsel > 0)
% hold the figure, so we can add the flagged points
hold on
if ~iscell(xselect)
flaghandle = plot(xselect,yselect,params.FlagMarker);
set(flaghandle,'Color',params.FlagColor,'MarkerFaceColor',params.FlagColor)
else
flaghandle = plot(vertcat(xselect{:}),vertcat(yselect{:}),params.FlagMarker);
set(flaghandle,'Color',params.FlagColor,'MarkerFaceColor',params.FlagColor)
end
% now release the hold
hold off
elseif ~isempty(flaghandle)
% otherwise, we just need to update xdata and ydata
if nsel == 0
set(flaghandle,'xdata',[],'ydata',[]);
elseif ~iscell(xselect)
set(flaghandle,'xdata',xselect,'ydata',yselect);
else
set(flaghandle,'xdata',vertcat(xselect{:}),'ydata',vertcat(yselect{:}));
end
end
end
% =====================================================
% =====================================================
function maketextlabels
% nested function for generation of text labels
% over each point selected
% We need to remove the last set of text labels
delete(texthandles)
% creat a new set of handles
if ~iscell(xselect)
xtext = xselect;
ytext = yselect;
else
xtext = vertcat(xselect{:});
ytext = vertcat(yselect{:});
end
% anything selected?
if ~isempty(xtext)
textlabels = cell(1,nsel);
for L = 1:nsel
textlabels{L} = ['(',num2str(xtext(L)),',',num2str(ytext(L)),')'];
end
texthandles = text(xtext,ytext,textlabels);
end
end
% =====================================================
end % mainline end
% ================================================
% end main function
% ================================================
% ================================================
% subfunctions
% ================================================
function [pl,xsel,ysel,nsel] = testpoly(xv,yv,xdata,ydata)
% checks which points are inside the given polygon
% was there more than one set of points found in the plot?
if ~iscell(xdata)
% only one set, so xdata and ydata are not cell arrays
% Which points from the data fall in the selection polygon?
pl = find(inpolygon(xdata,ydata,xv,yv));
nsel = length(pl);
xsel = xdata(pl);
ysel = ydata(pl);
else
% it was a cell array, so there were multiple sets.
pl = cell(size(xdata));
xsel = pl;
ysel = pl;
nsel = 0;
for i = 1:numel(xdata);
pl{i} = find(inpolygon(xdata{i},ydata{i},xv,yv));
nsel = nsel + length(pl{i});
if ~isempty(pl{i})
xsel{i} = xdata{i}(pl{i});
ysel{i} = ydata{i}(pl{i});
end
end
end
end % subfunction end
% ================================================
% subfunction
% ================================================
function [pointslist,xselect,yselect] = closestpoint(xy,xdata,ydata,dx,dy)
% find the single closest point to xy, in scaled units
if ~iscell(xdata)
% just one set of points to consider
D = sqrt(((xdata - xy(1))/dx).^2 + ((ydata - xy(2))/dy).^2);
[junk,pointslist] = min(D(:)); %#ok
xselect = xdata(pointslist);
yselect = ydata(pointslist);
else
% there is more than one set of points
Dmin = inf;
pointslist = cell(size(xdata));
for i = 1:numel(xdata);
D = sqrt(((xdata{i} - xy(1))/dx).^2 + ((ydata{i} - xy(2))/dy).^2);
[mind,ind] = min(D(:)); %#ok
if mind < Dmin
% searching for the closest point
Dmin = mind;
pointslist = cell(size(xdata));
xselect = cell(size(xdata));
yselect = cell(size(xdata));
pointslist{i} = ind;
xselect{i} = xdata{i}(ind);
yselect{i} = ydata{i}(ind);
end