Locate max by clicking on 2D surface
Show older comments
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
1 Comment
Igor Arkhandeev
on 5 Apr 2021
Answers (0)
Categories
Find more on Just for fun in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!