-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadddatatips.m
executable file
·82 lines (72 loc) · 2.74 KB
/
adddatatips.m
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
function [tiphdls,cursorMode] = adddatatips(plthdl,evtXes,useNearest)
%function [tiphdls,cursorMode] = adddatatips(plthdl,evtXes,useNearest)
%
% Add DATACURSORMODE data tips at X points EVTXES in plotted object with
% handle PLTHDL (e.g., the handle return value from PLOT). If USENEAREST
% (DEFAULT: True), X values in EVTEXES need not be exact matches (==) to
% those in the plot's XData property: in those cases, uses index returned by
% MIN(ABS(XDATA-EVTEXES(ix))), with a potential warning. If USENEAREST is
% instead a string beginning with the characters 'ind' (for index), then
% evtXes is interpreted as an array of indices in XDATA instead of values.
%
% Optionally returns an array of handles TIPHDLS for all datatips created,
% and the DATACURSOR manager object cursorMode.
%
% Assumes caller previously set tool tip appearances as desired (e.g., by
% calling SET_DATETICK_CURSOR). Based on code by "Hoki" originally found at:
% https://stackoverflow.com/questions/29882186/set-data-tips-programmatically
%
% CALLS: GETFIG to find the Figure handle associated with PLTHDL.
%
% Last Saved Time-stamp: <Wed 2017-05-24 13:15:54 Eastern Daylight Time gramer>
useIndices = false;
if ( ~exist('useNearest','var') )
useNearest = true;
elseif ( strcmpi(useNearest,'ind') )
useNearest = true;
useIndices = true;
end;
fh = getfig(plthdl);
if ( isempty(fh) )
error('No Figure handle found for plot handle!');
end;
% Retrieve the DATACURSOR manager
cursorMode = datacursormode(fh);
xdata = get(plthdl,'XData');
ydata = get(plthdl,'YData');
try,
zdata = get(plthdl,'ZData');
catch,
zdata = [];
end;
dx = median(diff(xdata));
% Add a datatip for each event
for ix = 1:numel(evtXes)
if ( useIndices )
idx = evtXes(ix);
else
% Find the index of the corresponding X-value
idx = find( xdata == evtXes(ix) ) ;
if ( isempty(idx) )
if ( ~useNearest )
error('No X-value matches event # %d: Try USENEAREST=True...',ix);
end;
[err,idx] = min(abs(xdata-evtXes(ix)));
if ( (idx == 1 || idx == numel(evtXes)) && err > (dx*2) )
warning('AddDataTips:Outlier','Event # %d may be outside of X range!',ix);
end;
end;
end;
if ( isempty(zdata) )
pos = [xdata(idx), ydata(idx), 0];
else
pos = [xdata(idx), ydata(idx), zdata(idx)];
end;
tiphdls(ix) = cursorMode.createDatatip(plthdl) ;
% Move it into the right place
set(tiphdls(ix), 'MarkerSize',5, 'MarkerFaceColor','none', ...
'MarkerEdgeColor','r', 'Marker','o', 'HitTest','off', ...
'Position',pos);
%update(tiphdls(ix), pos);
end;
return;