File Exchange

image thumbnail


version (11.8 KB) by Chad Greene
Get distances between points from mouse clicks on a figure.

1 Download

Updated 20 Nov 2014

View Version History

View License

gdist returns the graphical distance between click points from mouse. This function is a modified form of ginput, but differs in that gdist can return distance between each point or cumulative distance along a path of clicked points. gdist gives the user visual confirmation of clicked points, and points can be undone using the backspace key.
Data points are entered by pressing a mouse button or any key on the keyboard except the following:
Carriage return Terminates data entry.
Backspace Deletes previous data point.
z Zooms in, centered on current cursor location.
x Zooms out, centered on current cursor location.

EuclideanDistance = gdist
PathDist = gdist('path')
[...] = gdist(N)
[...] = gdist('color',ColorSpec)
[...,x,y] = gdist(...)
[...,x,y,h] = gdist(...)

EuclideanDistance = gdist gathers an unlimited number of points until the return key is pressed. Length of EuclideanDistance array is N-1 and is equivalent to EuclideanDistance = diff(PathDist).

PathDist = gdist('path') returns the distance traveled along a path of N clicks. Length of PathDist is equal to the number of clicks N, and the first first value in PathDist will always be zero. PathDist is equivalent to PathDist = [0;cumsum(EuclideanDistance)];

[...] = gdist(N) gets N points from the current axes and returns a vector containing the Euclidean distance from point to point.

[...] = gdist('color',ColorSpec) specifies color of the temporary lines and markers placed on the plot to assist the user.

[...,x,y] = gdist(...) returns x and y coordinates of clicked points.

[...,x,y,h] = gdist(...) returns a handle of line and marker objects clicked by the user. If a handle h is requested, the markers are not automatically removed after the point-entering process has terminated.

Any of the inputs described above can be combined in one argument. For example, after plotting some data like this:


you can simply type


to enter as many points as you'd like via mouse clicks. Or if you know ahead of time that you want to enter 4 points, type


or if you want to calculate the cumulative distance at each step along 4-point path, and you want the temporary guiding lines to be red, type


If you'd like to keep that red line in place after using gdist, request the handle h like this:

[pathdist,~,~,h] = gdist(4,'path','color','red');

Then you can delete the red line at your leisure by


For greater precision, type z to zoom in, or x to zoom out. Zooming in or out recenters the plot on the current mouse position. If you accidentally click a point you'd like to undo, click backspace.

Cite As

Chad Greene (2020). gdist (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (0)

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: gslope, MYGINPUT, ginput2.m v3.1 (Nov 2009), Custom GINPUT

Inspired: gdistm, clickz

Community Treasure Hunt

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

Start Hunting!