How do you set a default datatip callback function?
43 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(3 个)
Adam Danz
2021-10-27
编辑:Adam Danz
2021-10-27
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 个评论
Adam Danz
2021-11-1
@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.
Adam Danz
2021-11-1
- > I'm trying to add callback code to a scatterplot that activates when the use clicks on a data point
- > I want to set a default value for this callback function so it executes whenever any new datatip is created
- > 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.
0 个评论
Massimo Ciacci
2023-7-1
编辑:Massimo Ciacci
2023-7-1
%% setTips_CallBack.m
% call only when figure created and one line drawn, wo any tips
fh = gcf;
ax_hdl = gca;
% make sure there are NO dta tips yet
% this cursorMode will only work for every new clicked tip
cursorMode = datacursormode(fh);
axChild = get(ax_hdl,'Children');
allCurves = findobj(axChild,'type','line');
hTarget = allCurves(1);
% set format later read back from inside customDataTipCallBack as this:
% output_txt = customDataTipCallBack(src,evt)
% axh = ancestor(evt.Target,'axes');
% fmt_x = axh.UserData.fmt_x;
% fmt_y = axh.UserData.fmt_y;
fmt_x = '%2.2f';
fmt_y = '%2.2f';
chopY = 1e-7;
ax_hdl.UserData.fmt_x = fmt_x;
ax_hdl.UserData.fmt_y = fmt_y;
ax_hdl.UserData.chopY = chopY;
% register callback
set(cursorMode, 'UpdateFcn',@customDataTipCallBack);
% create a tip somewhere (no need)
% % hDatatip = cursorMode.createDatatip(hTarget);
1 个评论
Massimo Ciacci
2023-7-1
编辑:Massimo Ciacci
2023-7-1
Don't know why but the text I had written preceding the code is gone... Now I will be shorter.
The code above (setTips_CallBack.m) is is how I register my own callback function.
customDataTipCallBack can be found here:
setTips_CallBack.m comes with it's own test bench: test_setTips_CallBack where you create a plot and then with the mouse add tips using your custom callback.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!