NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/plots/marksafe.m

Revision 495 (checked in by cbc, 11 years ago)

Initial import of Stark code.

Line 
1 function marked = marksafe(varargin)
2
3 % marksafe -- Mark data points in (x, y) plot.
4 %  marksafe('demo') demonstrates itself, using random (x, y)
5 %   data plotted with EraseMode = 'xor'.
6 %  marksafe('on') enables marking, in which a mouse click
7 %   on a plotted (x, y) point marks the site with a symbol.
8 %   The z-data must be empty, or consist of TRUE (non-zero,
9 %   marked) and FALSE (zero, not marked) values.
10 %  marksafe(h) turns marking on for just the lines specified
11 %   by the handles h.
12 %  marksafe('off') turns marking off and removes the markers.
13 %   The z-data of the targetted lines remain marked with 1.
14 %  marksafe('clear') restores all the plotted data to visibility.
15 %  marked = marksafe(h) returns the mark-vector (1 = marked;
16 %   0 = not marked) for the line whose handle is h.
17 %
18 % Calls: none
19  
20 % Copyright (C) 2000 Dr. Charles R. Denham, ZYDECO.
21 %  All Rights Reserved.
22 %   Disclosure without explicit written consent from the
23 %    copyright owner does not constitute publication.
24  
25 % Version of 12-Jan-2000 09:44:58.
26 % Updated    18-Jan-2000 23:03:12.
27
28 MARKER_TAG = ['_' mfilename '_'];
29 MARKER_MARKER = '*';
30 MARKER_SIZE = get(0, 'DefaultLineMarkerSize');
31 MARKER_COLOR = [1 0 0];
32 MARKER_BUTTONDOWNFCN = mfilename;
33 MARKER_ERASEMODE = 'xor';
34
35 % Demonstration.
36
37 if nargin > 0 & isequal(varargin{1}, 'demo')
38         help(mfilename)
39         x = linspace(0, 1, 101);
40         y  = sin(3*pi*x);
41         noise = 2*(rand(size(x)) - 0.5);
42         noise(abs(noise) < 0.95) = 0;
43         h = plot(x, y+noise, '-o', ...
44                                 'MarkerSize', 4, 'EraseMode', 'xor');
45         eval(mfilename)
46         if exist('zoomsafe', 'file') == 2
47                 eval('zoomsafe')
48         end
49         set(gcf, 'Name', [mfilename ' demo'])
50         if nargout > 0, marked = h; end
51         return
52 end
53
54 % Get marks of a line.
55 %  Requires a handle and an output variable.
56
57 if nargout > 0 & nargin > 0
58         vis = [];
59         h = varargin{1};
60         if ischar(h), h = eval(h); end
61         if ishandle(h)
62                 if isequal(get(h, 'Type'), 'line')
63                         if ~isequal(get(h, 'Tag'), MARKER_TAG)
64                                 marked = ~~get(h, 'ZData');
65                         end
66                 end
67         end
68         return
69 end
70
71 % Actions: on, off, clear.
72
73 if nargin > 0 & isempty(gcbo)
74         if ischar(varargin{1})
75                 theAction = varargin{1};
76                 switch theAction
77                 case 'on'
78                         varargin = {};
79                 case 'off'
80                         h = findobj(gcf, 'Tag', MARKER_TAG);
81                         if any(h), delete(h), end
82                         return
83                 case 'clear'
84                         h = findobj(gcf, 'Tag', MARKER_TAG);
85                         if any(h)
86                                 for i = 1:length(h)
87                                         u = get(h(i), 'UserData');
88                                         z = get(u.handle, 'ZData');
89                                         z(u.index) = u.z;
90                                         set(u.handle, 'ZData', z)
91                                 end
92                                 delete(h)
93                         end
94                         return
95                 otherwise
96                         disp([' ## No such action: "' theAction '"'])
97                         return
98                 end
99         end
100 end
101
102 % Initialize.
103
104 if isempty(gcbo)
105         if isempty(varargin)
106                 h = findobj(gcf, 'Type', 'line');
107                 varargin{1} = h;
108         end
109         h = varargin{1};
110         set(h, 'ButtonDownFcn', MARKER_BUTTONDOWNFCN)
111         for i = 1:length(h)
112                 z = get(h(i), 'ZData');
113                 if isempty(z)
114                         x = get(h(i), 'XData');
115                         z = zeros(size(x));
116                         set(h(i), 'ZData', z);
117                 end
118                 f = find(z);
119                 make_marker(h(i), f)
120         end
121         return
122 end
123
124 % Process a click.  If clicking on the data, mark
125 %  the closest data point.  If clicking on a marker,
126 %  restore the corresponding point.
127
128 if isempty(varargin), varargin{1} = 'ButtonDownFcn'; end
129
130 if length(varargin) > 0 & ischar(varargin{1})
131         theEvent = varargin{1};
132         switch lower(theEvent)
133         case 'buttondownfcn'
134                 switch get(gcbo, 'Tag')
135                 case '_marksafe_'   % Delete the marker.
136                         u = get(gcbo, 'UserData');
137                         z = get(u.handle, 'ZData');
138                         z(u.index) = u.z;
139                         set(u.handle, 'ZData', z);
140                         delete(gcbo)
141                 otherwise   % Create a marker.
142                         p = get(gca, 'CurrentPoint');
143                         x0 = p(1, 1);
144                         y0 = p(1, 2);
145                         x = get(gcbo, 'XData');
146                         y = get(gcbo, 'YData');
147                         z = get(gcbo, 'ZData');
148                         if isempty(z), z = zeros(size(x)); end
149                        
150                         theMarker = get(gcbo, 'Marker');
151                         if isequal(theMarker, MARKER_MARKER)
152                                 MARKER_MARKER = 'o';
153                         end
154                         if ~isequal(get(gcbo, 'Marker'), 'none')
155                                 MARKER_SIZE = 2 + get(gcbo, 'MarkerSize');
156                         end
157                         if (1)
158                                 MARKER_ERASEMODE = get(gcbo, 'EraseMode');
159                         end
160                        
161                         dx = diff(get(gca, 'XLim'));   % Scale to pixels.
162                         dy = diff(get(gca, 'YLim'));
163                         theOldUnits = get(gca, 'Units');
164                         set(gca, 'Units', 'pixels');
165                         thePosition = get(gca, 'Position');
166                         theWidth = thePosition(3);
167                         theHeight = thePosition(4);
168                         dx = dx / theWidth;
169                         dy = dy / theHeight;
170                         set(gca, 'Units', theOldUnits)
171                        
172                         d = abs((x*dy+sqrt(-1)*y*dx - (x0*dy+sqrt(-1)*y0*dx)));
173                        
174                         index = find(d == min(d));
175                         if any(index)
176                                 index = index(1);
177                                 if ~z(index) & 0
178                                         u.handle = gcbo;
179                                         u.index = index;
180                                         u.z = z(index);
181                                         h = line(x(index), y(index),...
182                                                         'Marker', MARKER_MARKER, ...
183                                                         'MarkerSize', MARKER_SIZE, ...
184                                                         'MarkerFaceColor', 'none', ...
185                                                         'MarkerEdgeColor', MARKER_COLOR, ...
186                                                         'EraseMode', MARKER_ERASEMODE, ...
187                                                         'ButtonDownFcn', MARKER_BUTTONDOWNFCN, ...
188                                                         'UserData', u, ...
189                                                         'Tag', MARKER_TAG);
190                                         z(index) = 1;
191                                         set(gcbo, 'ZData', z)
192                                 else
193                                         h = make_marker(gcbo, index);
194                                 end
195                         end
196                 end
197         otherwise
198                 disp(['## No such event: ' theEvent])
199         end
200 end
201
202
203 % ---------- make_marker ---------- %
204
205 function theResult = make_marker(h, index)
206
207 % make_marker -- Create a marker.
208 %  make_marker(h, index) places a mark on line h
209 %   at the index, on behalf of the given mfile.
210  
211 % Copyright (C) 2000 Dr. Charles R. Denham, ZYDECO.
212 %  All Rights Reserved.
213 %   Disclosure without explicit written consent from the
214 %    copyright owner does not constitute publication.
215  
216 % Version of 18-Jan-2000 14:11:55.
217 % Updated    18-Jan-2000 23:03:12.
218
219 MARKER_TAG = ['_' mfilename '_'];
220 MARKER_MARKER = '*';
221 MARKER_SIZE = get(0, 'DefaultLineMarkerSize');
222 MARKER_COLOR = [1 0 0];
223 MARKER_BUTTONDOWNFCN = mfilename;
224 MARKER_ERASEMODE = 'xor';
225
226 x = get(h, 'XData');
227 y = get(h, 'YData');
228 z = get(h, 'ZData');
229 if isempty(z)
230         z = zeros(size(x));
231 end
232
233 theMarker = get(h, 'Marker');
234 if isequal(theMarker, MARKER_MARKER)
235         MARKER_MARKER = 'o';
236 end
237 if ~isequal(get(h, 'Marker'), 'none')
238         MARKER_SIZE = 2 + get(h, 'MarkerSize');
239 end
240
241 if (1), MARKER_ERASEMODE = get(h, 'EraseMode');end
242
243 result = zeros(size(index));
244
245 for i = 1:length(index)
246         u.handle = h;
247         u.index = index;
248         u.z = z(index);
249         result(i) = line(x(index(i)), y(index(i)),...
250                         'Marker', MARKER_MARKER, ...
251                         'MarkerSize', MARKER_SIZE, ...
252                         'MarkerFaceColor', 'none', ...
253                         'MarkerEdgeColor', MARKER_COLOR, ...
254                         'EraseMode', MARKER_ERASEMODE, ...
255                         'ButtonDownFcn', MARKER_BUTTONDOWNFCN, ...
256                         'UserData', u, ...
257                         'Tag', MARKER_TAG);
258         z(index(i)) = 1;
259 end
260
261 set(h, 'ZData', z)
262
263 if nargout > 0, theResult = result; end
Note: See TracBrowser for help on using the browser.