How do you set a default datatip callback function?

16 views (last 30 days)
Justin Dietz
Justin Dietz on 27 Oct 2021
Answered: Adam Danz on 1 Nov 2021
I'm trying to add callback code to a scatterplot that activates when the use clicks on a data point. To do this, I want to assign the callback to the creation function of a datatip. The datatip documentation says that datatips have a creation callback. I want to set a default value for this callback function so it executes whenever any new datatip is created. However, when I try to set this default value as described here, I get an error that says "datatip is an invalid class name".
My primary question is how do I assign a callback function that is executed whenever any datatip is created. A more general question is, what's the best way to set code to execute whenever the user clicks on an existing data point in a figure? Lines are objects that have properties, but the data points themselves are not.
Code to recreate the error:
x=1:10;
y=1:10;
figure
hold on
scatter(x,y,'filled');
hold off
set(groot,'defaultDataTipCreateFcn',@tipcreate);
function tipcreate(~,~)
disp('Hello World!');
end

Answers (2)

Adam Danz
Adam Danz on 27 Oct 2021
Edited: Adam Danz on 27 Oct 2021
It doesn't appear that there is a factory CreateFcn for Data Tips and its default value is an empty character array ('').
The list below shows all factory options for CreateFcn functions.
You could, of course, define a function in an m-file, add the file to the path in startup.m, and assign the function to all datatips. But I realize that isn't as ideal as setting a default function to be applied to all of your existing datatips. You could also suggest making this feature available [Contact tech support].
List of factory CreateFcn functions
factoryAnimatedlineCreateFcn
factoryAreaCreateFcn
factoryArrowshapeCreateFcn
factoryAxesCreateFcn
factoryAxestoolbarCreateFcn
factoryBarCreateFcn
factoryBinscatterCreateFcn
factoryBubblechartCreateFcn
factoryBubblelegendCreateFcn
factoryCategoricalhistogramCreateFcn
factoryColorbarCreateFcn
factoryConstantlineCreateFcn
factoryContourCreateFcn
factoryDoubleendarrowshapeCreateFcn
factoryEllipseshapeCreateFcn
factoryErrorbarCreateFcn
factoryFigureCreateFcn
factoryFunctioncontourCreateFcn
factoryFunctionlineCreateFcn
factoryFunctionsurfaceCreateFcn
factoryGeoaxesCreateFcn
factoryGraphplotCreateFcn
factoryHggroupCreateFcn
factoryHgjavacomponentCreateFcn
factoryHgtransformCreateFcn
factoryHistogram2CreateFcn
factoryHistogramCreateFcn
factoryImageCreateFcn
factoryImplicitfunctionlineCreateFcn
factoryImplicitfunctionsurfaceCreateFcn
factoryLegendCreateFcn
factoryLightCreateFcn
factoryLineCreateFcn
factoryLineshapeCreateFcn
factoryParameterizedfunctionlineCreateFcn
factoryParameterizedfunctionsurfaceCreateFcn
factoryPatchCreateFcn
factoryPolaraxesCreateFcn
factoryQuiverCreateFcn
factoryRectangleCreateFcn
factoryRectangleshapeCreateFcn
factoryScatterCreateFcn
factoryStairCreateFcn
factoryStemCreateFcn
factorySurfaceCreateFcn
factoryTextCreateFcn
factoryTextarrowshapeCreateFcn
factoryTextboxshapeCreateFcn
factoryTiledlayoutCreateFcn
factoryUibuttongroupCreateFcn
factoryUicontainerCreateFcn
factoryUicontextmenuCreateFcn
factoryUicontrolCreateFcn
factoryUiflowcontainerCreateFcn
factoryUigridcontainerCreateFcn
factoryUimenuCreateFcn
factoryUipanelCreateFcn
factoryUipushtoolCreateFcn
factoryUisplittoolCreateFcn
factoryUitabCreateFcn
factoryUitabgroupCreateFcn
factoryUitableCreateFcn
factoryUitogglesplittoolCreateFcn
factoryUitoggletoolCreateFcn
factoryUitoolbarCreateFcn
  4 Comments
Adam Danz
Adam Danz on 1 Nov 2021
@Justin Dietz, after re-reading your question, I don't think you really want the CreateFcn which is called when the datatip object is created which is why you must define that function within the same line that you define the datatip. I'm going to add a new answer.

Sign in to comment.


Adam Danz
Adam Danz on 1 Nov 2021
  1. > I'm trying to add callback code to a scatterplot that activates when the use clicks on a data point
  2. > I want to set a default value for this callback function so it executes whenever any new datatip is created
  3. > My primary question is how do I assign a callback function that is executed whenever any datatip is created
These are two different questions so the goal is not clear.
In question 1, you want a function that responds to pressing a scatter plot object. That would involve assigning a ButtonDownFcn to the scatter object.
In questions 2 and 3, you want a datatip to appear when you click a scatter object which would prevent use of the ButtonDownFcn (you can't have both).
It sounds like you want a datatip to appear when you click a scatter point and you want some function to be evoked as well. If so, the CreateFcn is not what you're looking for since that is evoked when a datatip object is created which isn't the same as when a datatip object appears after clicking on a point (that's why the CreateFcn must be defined when creating the datatip, otherwise it has no effect). Instead, you want the UpdateFcn of the DataCursorManager or you could use the DataTipTemplate depending on what you want to do. To understand the difference between the two, see Setting precision of datatip text.
There are lots of demos listed here and in the documentation. But I don't think there are default options for these functions, however. If there's a common function you'd like to apply, you could create the function in a separately m-file and then assign the function to all of your functions/scripts.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!