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
txt = {['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))]};
uiresume(gcf)
end
More information on the functions used above can be found here: