Generating a plot from selected figure point

14 views (last 30 days)
Hello,
I'm a relatively new matlab user and not too used to mouse click-related functions. What I have is a scatterplot of 2 parameters obtained by computations done on a series of waveforms.
What I want is, whenever I click a point on the plot, for a plot of the waveform corresponding to this point to appear in the secondary boxed figure. In order to do that, I'd like to get the position of either the x or y values (since it would be the same for both) of the point within the array that was used to create the scatterplot.
Following that, I can easily find the corresponding waveform. Getting there however, is the point I struggle with:
- I'm not sure how to code the selection by mouseclick. I find the documentation a bit obscure for a first-time use.
- I want the waveform plot to update every time a new point is selected, replacing the previous one. I don't need to do anything in particular for that, do I?
- I can't simply take the coordinates of the points since several points overlap and it doesn't tell me which waveform it came from.
Also, if you think you have a better idea of how to go about this, do tell!

Accepted Answer

Ameer Hamza
Ameer Hamza on 13 Mar 2020
You can use the ButtonDownFcn callback: https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#budumk7_sep_shared-ButtonDownFcn and CurrentPoint property: https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#budumk7-CurrentPoint of axes object to find out which point was clicked by the user. For example, try this
ax = axes();
ax.ButtonDownFcn = @cb;
cb is defined in cb.m file as
function cb(ax,~)
disp(ax.CurrentPoint);
end
Although it does not give the index in your data points, you can find the closed point in your dataset to the CurrentPoint (e.g., use Euclidean distance) and then plot on small axes accordingly.
  3 Comments
Ameer Hamza
Ameer Hamza on 17 Mar 2020
Yes, I mentioned in the answer that it would not give the coordinates of your data points. But you can find the closest point. For example, I have datapoints
x = [0.5 0.5;
0.5 0.6;
0.6 0.6]
So if the callback return
y = [0.55 0.45]
then i can use pdist2 to find the corresponding point in x
[~, index] = min(pdist2(x,y))

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!