MATLAB: How to display Index of a data point on the Data Cursor

data cursordatacursormodedisplay index on data cursorplot

When I use the Data Cursor option on a plot, I am able to view the X and Y-coordinates on the tool tip. However I wish to see the index of that point. I have tried editing the textUpdate function as follows:
function output_txt = myfunction(obj,event_obj)
% Display the position of the data cursor
% obj Currently not used (empty)
% event_obj Handle to event object
% output_txt Data cursor text string (string or cell array of strings).
pos = get(event_obj,'Position');
x = pos(1);
y = pos(2);
idx = find(xydata == x,y);
[row,~] = ind2sub(size(xydata),idx);
output_txt{end+1} = cell2mat(labels(row));
However this gives an error. Does anyone have any suggestions?
Thanks in advance
Deepa

Best Answer

I've received the desired output with the following code:
function txt = myupdatefcn(~,event_obj,myarray)
pos = get(event_obj,'Position');
ind = find(myarray(:,pos(1))== pos(2));
txt = {['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))],...
['Index: ',num2str(ind')]};
end
as well as the following code in the main function.
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',{@myupdatefcn,myarray})
Many thanks to everyone who helped.