[datacursermode] Counter for data tips in a figure

Hello everybody
I would like to have some kind of counter in my figure, that displays the number of data tips in my figure in real time. If I add a data tip the counter has to raise and if I delete a data tip the counter should decline.
Thanks in advance

回答 (2 件)

Fritz
Fritz 2014 年 10 月 31 日

0 投票

Here's some code, that does not work properly. Perhaps somebody knows why? Sometimes the counter miscounts and the performance of "Rotate 3D" decreases, if the number of datatips increases.
function DataTipCounter
% Example Patch Plot
[X,Y] = meshgrid(-2:0.2:2,-2:0.2:2);
Z = X.*exp(-X.^2 - Y.^2);
figure
h = surface(X,Y,Z);
patch(surf2patch(h))
delete(h)
shading faceted; view(3)
% Show figure toolbar
set(gcf,'toolbar','figure');
% Change the texboxes of the datatips
datacursormode on;
cursorMode = datacursormode(gcf);
set(cursorMode,'UpdateFcn',@myfunction)
% Button to switch Datacursormode On/Off
uicontrol('Style','pushbutton','Units','normalized','Position',...
[0.01 0.94 0.2 0.05],'String','Datacursor Switch','Callback','datacursormode',...
'FontUnits','normalized','FontSize',0.4)
function [output_txt, count] = myfunction(~,event_obj)
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
persistent datatip_objects %handles to the datatips on the graph
n = numel(datatip_objects);
found_match = 0;
count = 0;
%Look for the current datatip in our list of known datatips
while ~found_match && count < n
count = count+1;
found_match = (event_obj == datatip_objects{count});
end
uicontrol('Style','text','Units','normalized','Position',...
[0.79 0.94 0.2 0.05],'String', ['DataTips: ' num2str(count)],...
'FontUnits','normalized','FontSize',0.8)
if found_match
else
datatip_objects{n+1}=event_obj; %this datatip is new, store its handle
end
% Code from the standard 'Update Function'
output_txt = {count};
end
datacursormode off;
end
Thanks in advance
Sean de Wolski
Sean de Wolski 2014 年 10 月 31 日

0 投票

In R2014b, you can look for found parts of the axes that are PointDataTips
fig = figure;
ax = gca;
plot(rand(1,10))
% add some data tips
ntip = 0;
h = findall(ax);
for ii = 1:numel(h)
ntip = ntip+isa(h(ii),'matlab.graphics.shape.internal.PointDataTip');
end
disp(ntip)
There's probably a similar thing you can search for in releases before 14b.

カテゴリ

ヘルプ センター および File ExchangeEnvironment and Settings についてさらに検索

質問済み:

2014 年 5 月 6 日

回答済み:

2014 年 10 月 31 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by