Why does using 'ButtonDownFcn' function disable datatip?

조회 수: 19 (최근 30일)
Meredith Hedtke
Meredith Hedtke 2022년 8월 7일
편집: Adam Danz 2022년 8월 8일
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

답변 (1개)

Adam Danz
Adam Danz 2022년 8월 7일
편집: Adam Danz 2022년 8월 8일
> 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.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by