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

8 ビュー (過去 30 日間)
Julian Ostertag 2023 年 5 月 19 日
コメント済み: Julian Ostertag 2023 年 5 月 22 日
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.

サインインしてコメントする。

### 採用された回答

Allen 2023 年 5 月 19 日
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 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Julian Ostertag 2023 年 5 月 22 日
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 :)

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by