How to find data displayed in pcolor using coordinates?

6 ビュー (過去 30 日間)
Benoit Espinola
Benoit Espinola 2021 年 3 月 18 日
コメント済み: Benoit Espinola 2021 年 3 月 18 日
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 Center および File ExchangeGraphics Object Programming についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by