Why does using 'ButtonDownFcn' function disable datatip?

5 views (last 30 days)
I currently have a plot that has a custom data tip. My goal is to be able to have the user click on a point on the plot (which displays the datatip) and be able to store the coordinate of the datatip as well as create another plot. I went about this by creating a ButtonDownFcn function to store the coordinate and then create another figure; however, when this happens my custom datatip doesn't show up anymore.
So for clarity I need to be able to read when the user clicks on a point and then:
  1. display a custom datatip
  2. store the coordinates of the datatip
  3. then create another plot
This is my code:
%Plots data
tbl = readtable("FINAL_898_AbdVagAntGast_FASCCILE1.xlsx");
s = scatter(tbl,'Var3','Var4','filled');
%%Creates custom datatip
datatip(s,'Visible','off');
s.DataTipTemplate.DataTipRows(1).Label = "X";
s.DataTipTemplate.DataTipRows(2).Label = "Y";
row = dataTipTextRow("Diameter",tbl.Var11);
row2 = dataTipTextRow("Myelin Thickness",tbl.Var13);
s.DataTipTemplate.DataTipRows(end+1) = row;
s.DataTipTemplate.DataTipRows(end+1) = row2;
%%Stores coordinate and creates second plot
set(s, 'ButtonDownFcn', @mousecon);
function mousecon(~,~)
p = get(gca,'CurrentPoint');
x = p(1);
y = p(2);
figure
tbl = readtable("Rawdata_898_AbdVagAntGast_FASCICLE1_UnmyelinatedAxons_2.xlsx");
scatter(tbl,"X","Y",'filled')
end

Answers (1)

Adam Danz
Adam Danz on 7 Aug 2022
Edited: Adam Danz on 8 Aug 2022
> Why does using 'ButtonDownFcn' function disable datatip?
When you click on an object, the object has no idea of your intentions. All it knows is that there was a click. Suppose you have a ButtonDownFcn that is designed to delete the object. How should the object decide whether to delete itself after a click or assign a data tip to the location of the click? Thus, when a ButtonDownFcn is assigned, datatips are disabled.
You can, however, set the UpdateFcn of datacursormode. The function will execute every time a datatip is added. See this answer for more info and examples.
> My goal is to be able to have the user click on a point on the plot (which displays the datatip) and be able to store the coordinate of the datatip as well as create another plot.
This answer reviews 5 methods to access data tip content.

Tags

Community Treasure Hunt

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

Start Hunting!