How do I extract custom data tip info

조회 수: 59 (최근 30일)
David K
David K 2023년 3월 15일
답변: Vinayak Gupta 2023년 4월 3일
I have created custom data tips for my plot using the DataTipTemplate functionality. When I add cursors to a plot they display nicely all of the information that I want. Now I want to extract all of the data for the selected cursors to the workspace, but when I select "Extract Cursor Data to Workspace..." or use getCursorInfo() I only get the plotted position, not all of the extra information I put in the data tip. How can I extract the custom data to the workspace as well?

답변 (1개)

Vinayak Gupta
Vinayak Gupta 2023년 4월 3일
Hi David
The DataTipTemplate functionality is only able to modify that datatip display. It cannot modify the internal data, which is Target, Position and DataIndex. I am assuming you have some code similar to:
% Create a plot with custom data tips
x = 1:10;
y = rand(1,10);
p = plot(x, y);
% DataTipTemplate Method
dtt = p.DataTipTemplate;
dtt.DataTipRows(3).Label = 'Product';
dtt.DataTipRows(3).Value = @(x,y)x*y;
We can create a workaround by writing a custom function:
function data = getCursor(dcm)
data = getCursorInfo(dcm);
for i=1:numel(data)
data(i).Product = data(i).Position(1) .* data(i).Position(2);
end
end
The function takes a datacursormanager object as the argument. Read more on DataCursorMode.
Also you can use data cursor to modify the datatips text
% DataCursor Update Method
dcm = datacursormode(gcf);
set(dcm,'UpdateFcn',@customTip);
function txt = customTip(obj,event_obj)
pos = get(event_obj,'Position');
txt = {...
['X: ', num2str(pos(1))], ...
['Y: ', num2str(pos(2))], ...
['Product: ', num2str(pos(1)*pos(2))] ...
};
end
In any case, we will need to write our custom function to get customdata from the datatips.

카테고리

Help CenterFile Exchange에서 Visual Exploration에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by