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".