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

조회 수: 2 (최근 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일
편집: 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에서 Simulink Environment Customization에 대해 자세히 알아보기

제품


릴리스

R2012b

Community Treasure Hunt

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

Start Hunting!

Translated by