How to get cursor information of data tips using call back of a push buttion?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a fairly simple code. All I want is user to select multiple data tips (Alt+left mouse button) from a plot and on clicking a push button, the position and data index of the data tips be written in a structure. The command, c_info = cursorMode.getCursorInfol; gives me the required output but not when used in a callback function. Please suggest a way to assign the above command to a push button. Thanks for the help! First draft of the code is herewith:
function [] = GUI_me(varargin)
fh = figure('units','pixels',...
'position',[200 100 500 560],...
'menubar','none',...
'name','GUI_1',...
'numbertitle','off',...
'resize','off');
z = peaks;
plot(z(:,30:35))
dcm_obj = datacursormode(fh);
set(dcm_obj,'DisplayStyle','datatip',...
'SnapToDataVertex','off','Enable','on')
pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Done',...
'callback',{@pb_call});
pb2 = uicontrol('style','push',...
'units','pix',...
'position',[300 10 180 40],...
'fontsize',14,...
'string','Clear data tips',...
'callback',{@pb2_call});
function [] = pb2_call(varargin) % Clears user selected data tips
cursorMode = datacursormode(gcf);
cursorMode.removeAllDataCursors();
function [] = pb_call(varargin) % Returns the position and data index of selected data tips
% Callback for pushbutton.
cursorMode = datacursormode(gcf);
c_info = cursorMode.getCursorInfo;
0 Kommentare
Antworten (1)
Sameer
am 30 Mai 2025
Hi @automycer
To retrieve data tip information using "getCursorInfo" inside a push button callback, it is important to ensure that the "datacursormode" is correctly associated with the figure and is active at the time of the callback.
In the provided code, this approach should work if the cursor mode is active and data tips have been selected before the push button is pressed. The key is to make sure "datacursormode" is enabled on the figure, and the figure handle used in the callback is correct.
Here is a slightly modified version of the push button callback that retrieves and stores the data tip information:
function [] = pb_call(varargin)
figHandle = gcf; % or store handle to 'fh' and use that
cursorMode = datacursormode(figHandle);
c_info = cursorMode.getCursorInfo;
% Example: Store the data tip info in a structure
if ~isempty(c_info)
for i = 1:length(c_info)
output(i).Position = c_info(i).Position;
output(i).DataIndex = c_info(i).DataIndex;
end
disp(output); % or use it as needed
else
disp('No data tips selected.');
end
end
This method allows storing the position and data index of all selected data tips when the button is clicked. If the function still does not return anything, it's recommended to check that data tips were selected before pressing the button and that "datacursormode" is active.
Hope this helps!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!