How do I get the x,y coordinates from the scatter graph I generated?

Hi, I need to get those values to put into an equation. Can anyone help me? Here is the example code that I wrote. Thanks.
xrange = 0:100;
yrange = 0:100;
a= randi(numel(xrange),15,1); %randomly choose xcoordinate
b = randi(numel(yrange),15,1); %randomly choose ycoordinate
scatter(a(:),b(:),2000,'r')
for k=1:numel(a,b)
text(a(k),b(k),['(' num2str(a(k)) ',' num2str(b(k)) ')'])
end

回答 (2 件)

Zoltán Csáti
Zoltán Csáti 2014 年 11 月 2 日
You created the scatter plot in view of vectors a and b. So those are the coordinates. Supposing you only have the scatter plot and you want to extract the coordinates:
sc = findobj(gca,'Type','hggroup');
xCoord = get(sc, 'XData');
yCoord = get(sc, 'YData');

6 件のコメント

ker fang fang
ker fang fang 2014 年 11 月 2 日
how do i change the variable? i need those coordinate in the graph to be put into an equation, say, f(x,y)= x+y^2.
Zoltán Csáti
Zoltán Csáti 2014 年 11 月 2 日
When you have the vectors xCoord and yCoord, you put into function f as follows:
f = xCoord + yCoord.^2;
ker fang fang
ker fang fang 2014 年 11 月 2 日
編集済み: Image Analyst 2014 年 11 月 2 日
but my program in terms of a,b. so how do i change it? is it like that?
sc = findobj(gca,'Type','hggroup');
a = get(sc, 'XData');
b = get(sc, 'YData');
f = a + b.^2;
Zoltán Csáti
Zoltán Csáti 2014 年 11 月 2 日
Yes.
ker fang fang
ker fang fang 2014 年 11 月 2 日
but there is no response or any answer by that equation, it seem like the program after the for loop, does not read it. how to solve it?
the code
if true
% code
xrange = 0:100;
yrange = 0:100;
a= randi(numel(xrange),15,1); %randomly choose xcoordinate
b = randi(numel(yrange),15,1); %randomly choose ycoordinate
scatter(a(:),b(:),2000,'r')
for k=1:numel(a,b)
text(a(k),b(k),['(' num2str(a(k)) ',' num2str(b(k)) ')'])
end
sc = findobj(gca,'Type','hggroup');
a = get(sc, 'XData');
b = get(sc, 'YData');
f = a + b.^2;
Zoltán Csáti
Zoltán Csáti 2014 年 11 月 2 日
I do not really know what you want. You have vectors a and b at disposal.

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

Image Analyst
Image Analyst 2014 年 11 月 2 日

0 投票

The answer to your question is that you already have "a" and "b" so you have the "x,y coordinates" that you're requesting. Like Zoltan already told you, there is no need to call get() to extract it from the plot when it was YOU who created the plot - you must have the coordinates to create the plot in the first place.
If you want some kind of interpolated surface for every value of a and b, even those where you don't have any training data, try scatteredInterpolant().

カテゴリ

タグ

質問済み:

2014 年 11 月 2 日

回答済み:

2014 年 11 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by