How to find data displayed in pcolor using coordinates?

6 次查看(过去 30 天)
Hi all,
I am trying to find the value displayed by pcolor in a specific coordinate.
rng(1)
z = randn(11);
x = -5:5;
y = -5:5;
[X, Y] = meshgrid(x,y);
xq = 1.25;
yq = 2.34;
pcolor(x,y,z);
hold on
scatter(xq,yq, 'k', 'filled');
How can I find the z-value at the point xq, yq?
I have been banging my head against the wall and can't figure it out.

采纳的回答

Benoit Espinola
Benoit Espinola 2021-3-18
rng(1 )
z = randn(11 );
x = -5:5 ;
y = -5:5 ;
[X, Y] = meshgrid(x,y );
xq = [1.25, -4.5 -4.5 -4.5 -4.5 -4.5 -4.1 -4.9, 4.5 -6];
yq = [2.34, -4.5 -3.5 -3.1 -3.9 -4.5 -4.5 -4.5, 4.5 -6];
hP = pcolor(x,y,z );
hold on
scatter(xq,yq, 'k', 'filled');
xlim([-6 6])
ylim([-6 6])
[~,Id_x] = min(abs(x'-xq));
flag = xq < x(Id_x);
Id_x(flag) = Id_x(flag)-1; % put it back in the "right square"
Id_x(Id_x<1) = []; %outside range
[~,Id_y] = min(abs(y'-yq));
flag = yq < y(Id_y);
Id_y(flag) = Id_y(flag)-1; % put it back in the "right square"
Id_y(Id_y<1) = []; %outside range
zq = z(Id_y', Id_x'); % note the coordinates here are inverted
zq = zq(:,1)'; % got lazy to find something more elegant
Paras Patel has shown me the right path to the solution. However, Matlab returns the nearest neighbour, which in some situations yields to the wrong "pixel".

更多回答(1 个)

Paras Patel
Paras Patel 2021-3-18
rng(1)
z = randn(11);
x = -5:5;
y = -5:5;
[X, Y] = meshgrid(x,y);
xq = 1.25;
yq = 2.34;
hP = pcolor(x,y,z);
hold on
scatter(xq,yq, 'k', 'filled');
dt = datatip(hP,0,0);
hP.DataTipTemplate.DataTipRows(3).Value = hP.CData;
One way to do this would be to insert a datatip on the image. See the code above - it's crude but simple.
  2 个评论
Benoit Espinola
Benoit Espinola 2021-3-18
Thanks, this has lead me to the right direction... I don't know why Mathworks doesn't offer somethig ready to query datapoints in a pcolor... go figure...
PS:, I know I should NOT use pcolor, but it's easy-ish
Benoit Espinola
Benoit Espinola 2021-3-18
With the datatip, Matlab returns the nearest neighbour, which in some situations yields to the wrong "pixel".

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by