MATLAB: Data Tip properties do not persist

data tipsdatacursormodeguiMATLAB

I made a custom data tip function to show in-depth info about points in a plot. It works as expected. However, I'm having trouble getting the properties of the text in the data tip popups to stick. Currently, I set up these tips as follows (following Walter Roberson's suggestion regarding hggroup objects):
dcm = datacursormode(h_fig);
set(dcm, 'UpdateFcn', {@myTooltip, myData});
% Set tooltip font to make it readable (lots of numbers!)
alldatacursors = findall(h_fig, 'type', 'hggroup', '-property', 'FontSize');
set(alldatacursors, 'FontSize', 14, 'FontName', 'Consolas');
Unfortunately, when I create a data tip by clicking on a point in the plot, the result is rendered in a default font and size (Helvetica 10?), not Consolas 14. Only if I manually execute the statements above a second time (either from the Command Window, or by highlighting them in the Editor and choosing Evaluate Selection from the context menu) does the font style/size get updated. Maddeningly, if I then create a second data tip, it comes up in the default font again.
Can anyone spot my error(s)? Am I simply mis-applying the datacursormode mechanism and/or hggroup properties?

Best Answer

Hi,
You can try pasting both the lines of code where the font size is initialized, inside the ‘UpdateFcn’ of the datacursormode function. This ensures the data tip properties persist. For example:
x = 1:10;
y = x.^2;
h_fig = figure;
scatter(x,y)
dcm = datacursormode;
dcm.Enable = 'on';
dcm.UpdateFcn = {@displayCoordinates,h_fig};
function txt = displayCoordinates(~,info,h_fig)
x = info.Position(1);
y = info.Position(2);
txt = ['(' num2str(x) ', ' num2str(y) ')'];
alldatacursors = findall(h_fig, 'type', 'hggroup', '-property', 'FontSize');
set(alldatacursors, 'FontSize', 20, 'FontName', 'Rockwell');
end
Hope this helps!