Clear Filters
Clear Filters

How can I infer from the data cursor position, the index of the closest datapoint

7 views (last 30 days)
Hi everyone,
I am working in the App Designer and I want to have a tool which lets the user select a bunch of datapoints. I have a UI Axes and defined a ButtonDownFcn.
Once the user clicks in the axis I basically want to trigger some sort of selection process. The goal is to select the datapoints from the first click up until the mouse is clicked a second time. It is not obvious to me how I can infer from the cursor position (which i can extract) which of my datapoints is closest. Lets say I have plotted 1000 random datapoints, I dont know how a cursor position of say 200 could give me any idea about my real values.
Maybe some of you have encountered a similar problem and can help, that would be really great. Also if there may be a better solution, I am happy to hear it.
Thank you very much!!
For the button down function i was thinking about something along the line of:
selectedRegions = [];
% Button down function: RawEEG_ax
app.ax.ButtonDownFcn = @(hObject, evt) axclick(hObject,evt);
function axclick(hObject,evt)
x = dataIdx1;
cp = hObject.CurrentPoint;
cx = cp(1,1);
if isempty(selectedRegions)
selectedRegions(1) = round(interp1(x, 1:numDataPoints, cx, 'nearest', 'extrap'));
else
selectedRegions(2) = round(interp1(x, 1:numDataPoints, cx, 'nearest', 'extrap'));
end
end
I know there is also this datacursor mode, but I was hoping to find a better solution than using this, since I actually do not just want the indexes of te datapoints, but was hoping to have some more functionality once I have solved this first problem, e,g, live tracking of the cursor to visualize the selected region and I think for that purpose the datacursor mode is not optimal, so I wanted to try something else.

Accepted Answer

Allen
Allen on 19 May 2023
Julian,
What you are asking for sounds very much like what the brush data tool does. However, there are a few extra steps required to extract the data from the brushed data points. The following links provide guidance on this tool and how to extract data.
Brush Data Tool
Extracting data values from brushed points
  1 Comment
Julian Ostertag
Julian Ostertag on 22 May 2023
Hi Allen,
thank you very much. Indeed it sounds like it would fit. However it did not work for me (but probably because my skills were not good enough). However I found when the xaxis is simply a vector from 1 to N, with N being the number of elements, then one can easily infer from the cursor position the x value. I had a more complicated xaxis before and therfore missed this. Thank you anyway :)

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!