Array = linspace(0, 2*pi, 200);
x = cos(Array);
y = sin(Array);
plot(x,y, 'bo-')
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
promptMessage = sprintf('Click two points,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
[twoPointsX, twoPointsY] = ginput(2)
hold on;
plot(twoPointsX,twoPointsY, 'rd')
distances = sqrt((x - twoPointsX(1)) .^2 + (y - twoPointsY(1)) .^ 2)
[minDistance1, index1] = min(distances)
distances = sqrt((x - twoPointsX(2)) .^2 + (y - twoPointsY(2)) .^ 2)
[minDistance2, index2] = min(distances)
plot([x(index1), x(index2)], [y(index1), y(index2)], 'rs-')
distance = sqrt( (x(index1)-x(index2))^2 + (y(index1)-y(index2))^2)
message = sprintf('The distance = %f', distance);
uiwait(helpdlg(message));
Best Answer