How to count data cursors and assign a fixed number to each?

2 views (last 30 days)
Dear all,
I had some code that worked relatively well on previous versions of Matlab (see below). Basically, I was using a custom data cursor function where I could count the number of cursors on the plot and each cursor had its own identifying number (for three cursors on the plot, I knew how many there were and each one was numbered 1 to 3 according to the order of creation. This was great, as I could then move a given data cursor around and it would not change the identifying number.
After I updated to 2016a it is not working anymore. Specifically, instead of keeping one number for each cursor, now it increases the number every time I move a given cursor around.
Does any one know what changed in this version that made it not work anymore, and how can I restore this function?
function txt = AddC(~,event_obj)
% Customizes text of data tips
% 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
if found_match;
else
datatip_objects{n+1} = event_obj; %this datatip is new, store its handle
end
uicontrol('Style','text','Units','normalized','Position',...
[0.79 0.90 0.2 0.05],'String', ['Cell number: ' num2str(count)],...
'FontUnits','normalized','FontSize',0.8)
uicontrol('Style','text','Units','normalized','Position',...
[0.79 0.95 0.2 0.05],'String', ['Number of cells: ' num2str(n)],...
'FontUnits','normalized','FontSize',0.8)
pos = get(event_obj,'Position');
txt = {['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))],...
['N: ',num2str(count+1)]};
end

Answers (2)

Joseph Cheng
Joseph Cheng on 10 Jun 2016
Edited: Joseph Cheng on 10 Jun 2016
you can change things around and use something like this:
hfig = figure;plot(1:10,[1:10].^2)
hcursor = datacursormode(hfig)
%pause here to add data points
getCursorInfo(hcursor);
not knowing how you're envoking your function i would have it changed to just query all the data points that show up instead. newest data points will be added to first index position shifting everything to the right. you can keep your persistance since you know that if you move it it'll update the value so you can check to see which one moved or was added.

KMC
KMC on 10 Jun 2016
Dear Joseph,
Yeah, I evoke the function using a very similar code, and it still retains its order in the cursor_info vector, but the issue is that I want to see the numbering of the data cursors as I'm adding them (I need to add a few dozen data points and it can get confusing is I can't tell them apart), so it doesn't really solve the problem.
Thanks for replying though. I also tried to see if I could invoke the cursor_info vector interactively during the point adding, but couldn't get that to work really.
  1 Comment
Samuli Karvinen
Samuli Karvinen on 15 Jan 2020
Dear KMC,
Were you able to figure this out? I'm also searching for an answer for this and I can't find any help online.
Thanks in advance!
Samuli

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!