Extracting data from part of a plot

6 views (last 30 days)
I want to extract data point from a part of a plot. If I plot my data I can see the data points are distributed in groups or cluster. I want to extract one such cluster for further processing.
For example, lets look at the following code.
x=[1,10,20,3,11,23,4,12,13,13,24]+rand(1,11);
y=[2,7,21,1,8,24,3,9,8,10,22]+rand(1,11);
plot(x,y,'ro')
Now suppose I want to extract the data points of the middle group for further processing. Is it possible in matlab, if yes how can I do it?

Accepted Answer

Apurba Paul
Apurba Paul on 8 Aug 2022
I have figured out a way. You can actually select data direclty from plot using Brush/Select data tools, whcich can be accessed through the top right corner of the current axis. You can then drag and select any part of the data, and then you can copy, delete or manipulate the data. See the figure bellow.
For more detail see the following link:

More Answers (1)

Walter Roberson
Walter Roberson on 6 Aug 2022
Do they need to be extracted from the plot or can we use x and y instead?
Is the number of groups known? This code assumes that it is.
x=[1,10,20,3,11,23,4,12,13,13,24]+rand(1,11);
y=[2,7,21,1,8,24,3,9,8,10,22]+rand(1,11);
num_groups = 3;
%cluster them to figure out which ones belong together
[IDX, C] = kmeans([x(:), y(:)], num_groups);
%but which one is the "center" group? Find the center that
%is closest to the centroid of the data ?
D = pdist2(C, [mean(x(:)), mean(y(:))]);
[~, centeridx] = min(D);
%extract items that are in that center group
xc = x(IDX == centeridx);
yc = y(IDX == centeridx);
scatter(xc(:), yc(:), 'b+')
xlim([min(x)-1, max(x)+1])
ylim([min(y)-1, max(y)+1])
  1 Comment
Apurba Paul
Apurba Paul on 8 Aug 2022
@Walter Roberson, this seems a nice idea to use kmeans to find the cluster. But for this perticular workflow, it will be much easier for me if I can direcltly select the data from plot. Thankfully I have figured out a way. You can actually select data direclty from plot using Brush/Select data tools, whcich can be accessed through the top right corner of the current axis. You can then drag and select any part of the data, and then you can copy, delete or manipulate the data. See the figure bellow.
For more detail see the following link:

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!