Locate max by clicking on 2D surface

2 次查看(过去 30 天)
Hello! I try to write some siple code that should find and mark maximum in defining area. I have some code, but I don't understand. why marker is always righter than I click. I will be happy for any proposals.
close all;
f = figure;
s = surf(peaks);
view([0 90]);
shading interp;
GetMousePosition(f,'on');
s.ButtonDownFcn = @get_data_from_clicking;
function get_data_from_clicking(~, ~)
coords = get(gca, 'CurrentPoint');
[x,y] = get_xy(coords(1,1), coords(1,2));
[xx,yy] = find_maximum(x,y,peaks);
hold on;
scatter3(xx, yy, 10^4, 'filled');
end
function [xx,yy] = get_xy(x, y)
xx = floor(x);
dx = xx + 0.5;
if dx - x < 0; xx = dx; end
yy = floor(y);
end
function [xx,yy] = find_maximum(x,y,mat)
xx = 0; yy = 0;
x_start = 2*x - 1;
y_start = y - 2;
I = 0;
for i = 0:2
for j = 0:2
if mat(x_start + i, y_start + j) > I
xx = x_start + i;
yy = y_start + j;
I = mat(x_start + i, y_start + j);
end
end
end
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by