How can I enable the cursor mode only on a subplot in my figure?
2 views (last 30 days)
Show older comments
MathWorks Support Team
on 9 May 2013
Edited: MathWorks Support Team
on 17 Feb 2021
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.
Accepted Answer
MathWorks Support Team
on 17 Feb 2021
Edited: MathWorks Support Team
on 17 Feb 2021
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 Comments
More Answers (0)
See Also
Categories
Find more on Simulink Environment Customization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!