How to find data displayed in pcolor using coordinates?

5 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Benoit Espinola
Benoit Espinola il 18 Mar 2021
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".

Più risposte (1)

Paras Patel
Paras Patel il 18 Mar 2021
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 Commenti
Benoit Espinola
Benoit Espinola il 18 Mar 2021
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 il 18 Mar 2021
With the datatip, Matlab returns the nearest neighbour, which in some situations yields to the wrong "pixel".

Accedi per commentare.

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by