How can I enable the cursor mode only on a subplot in my figure?

7 次查看(过去 30 天)
I have a figure with four subplots. I would like to use the data cursor to select points from one subplot, not the other ones. If the user by mistake selects points from the other subplots, I want to be able to discard them.

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2021-2-17
Here are some steps that show you how you can proceed:
1. Plot a figure with four subplots
2. Create two empty arrays where you will later store the coordinates of the data points picked from the target axes.
3. Create a DATACURSORMODE object on the current figure. Set it to 'on' and create a customized callback function 'pickData' (see further below) for 'UpdateFcn', as follows:
dcm_obj = datacursormode(gcf);
set(dcm_obj,'Enable','on');
set(dcm_obj,'UpdateFcn',@pickData)
4. Say you want to pick "N" points in total. Create a WHILE loop where you decrease the counter that keeps track of the number of points picked. The loop calls the function UIWAIT which blocks the execution of the program, hence allowing the user to select points. The programs resumes when UIRESUME is encountered.
counter = N;
while counter
uiwait(gcf)
counter = counter - 1;
end
5. The callback function 'pickData' is used a wrapper, since we actually do not need to update the way the data tips are displayed, but rather, we use its body to run other commands. specifcally, in 'pickData’, we do a test on the axes handle 'hTarget' and the parent of the data cursor 'cTarget', below.
If they match, the points are selected and stored. If not, the points are discarded, and the loop continues until ‘counter’ points have been selected with the data tip.
function txt = pickData(~,event_obj)
pos = get(event_obj,'Position');
cTarget = get(event_obj.Target,'parent');
if isequal(cTarget,targetAxes)
xData(end+1) = pos(1);
yData(end+1) = pos(2);
end
% We really do not need 'txt'. On can set it to ''.
txt = {['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))]};
uiresume(gcf)
end
More information on the functions used above can be found here:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Axes Appearance 的更多信息

产品


版本

R2012b

Community Treasure Hunt

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

Start Hunting!

Translated by