필터 지우기
필터 지우기

How to find data displayed in pcolor using coordinates?

조회 수: 3 (최근 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 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