How do I get rid of custom data tip?

I have this script which adds a custom data tip to my scatter3 graph :
function txt = displayCoordinates(~, info)
global clickedCIE_L clickedCIE_a clickedCIE_b;
clickedCIE_L = info.Position(3); % 69
clickedCIE_a = info.Position(1); % -56
clickedCIE_b = info.Position(2); % 46
Hrad = mod(atan2(clickedCIE_b,clickedCIE_a),2*pi); % 2.4539
hueAngle = Hrad*180/pi; % 140 degrés
chroma = sqrt(clickedCIE_a.^2 + clickedCIE_b.^2); % 72
txt = {sprintf('CIE L: %.0f', info.Position(3)), sprintf('CIE a: %.0f', info.Position(1)), sprintf('CIE b: %.0f', info.Position(2)), sprintf('CIE c : %.0f ', chroma), sprintf('CIE h : %.0f °', hueAngle) };
end
It works great, producing this output :
Problem is, this custom data tip does not have any handle I can see? So I can't make the data tip disappears programmatically when it is no longer needed? Suppose I click on the pushbutton 'Plot2D' : I wish there was away to delete the data tip....
Any help is appreciated.

Answers (1)

Voss
Voss on 19 Mar 2022
Edited: Voss on 19 Mar 2022
If feasible, you can store the handle to the DataTip when it is created (Option 1 below).
If not, you can find it using findobj() (Option 2 below).
Then delete() it.
h = plot(1:10);
h.DataTipTemplate.DataTipRows(1).Label = 'Custom X';
h.DataTipTemplate.DataTipRows(2).Label = 'Custom Y';
% Option 1: store the handle to the DataTip when it is created:
dt = datatip(h)
dt =
DataTip (Custom X 1, Custom Y 1) with properties: DataIndex: 1 Location: 'northeast' Parent: [1×1 Line] Show all properties
% Option 2: find the DataTip after it is created, using findobj():
dt = findobj(gca(),'Type','DataTip')
dt =
DataTip (Custom X 1, Custom Y 1) with properties: DataIndex: 1 Location: 'northeast' Parent: [1×1 Line] Show all properties
% now that you have the handle, delete the DataTip:
delete(dt);

5 Comments

Your approach supposes I use a different method of data tip creation (DataTipTemplate) which I used at some point in the development of myu humble script but it did not give me the flexibility of the above method? I infer from your answer that it is not feasible...
Thank you so very much to take the time to spell out your answer which would work perfect if I was to use a DataTipTemplate. I have some old code I can probably dig out to show you but last time I tried, I wasn't able to achieve the same results? I was told I could not change the default order in which the data was presented in the tool tip? Had to be in XYZ order and I needed to change it to ZXY order...
Voss
Voss on 19 Mar 2022
Edited: Voss on 19 Mar 2022
I used DataTipTemplate in my answer, but findobj() will work however the DataTip is created.
The creation of the actual DataTip is missing from your question, as the function you posted returns a cell array of character vectors but doesn't show how that is used to make a DataTip.
Exactly, I have no idea how the data tip is created?
Well, maybe try putting these lines:
dt = findobj(gca(),'Type','DataTip');
delete(dt);
Or more succinctly:
delete(findobj(gca(),'Type','DataTip'));
wherever you want to delete any DataTips from the current axes, and see if that works.
Great! thanks!!

Sign in to comment.

Products

Release

R2021a

Asked:

on 19 Mar 2022

Commented:

on 14 Jan 2025

Community Treasure Hunt

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

Start Hunting!