File Exchange

image thumbnail


version (4.56 KB) by jiro
GINPUT with customizable cursor.


Updated 01 Sep 2016

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This function behaves similarly to MATLAB's GINPUT, with an added ability to customize the cursor color, line width, and line style. From the function HELP:
[X,Y] = ginputc(N) gets N points from the current axes and returns the X- and Y-coordinates in length N vectors X and Y. The cursor can be positioned using a mouse. Data points are entered by pressing a mouse button or any key on the keyboard except carriage return, which terminates the input before N points are entered. Note: if there are multiple axes in the figure, use mouse clicks instead of key presses. Key presses may not select the axes where the cursor is.

[X,Y] = ginputc gathers an unlimited number of points until the return key is pressed.

[X,Y] = ginputc(N, PARAM, VALUE) and [X,Y] = ginputc(PARAM, VALUE) specifies additional parameters for customizing. Valid values for PARAM are:
'Color' : A three-element RGB vector, or one of the MATLAB predefined names, specifying the line color. See the ColorSpec reference page for more information on specifying color. Default is 'k' (black).
'LineWidth' : A scalar number specifying the line width. Default is 0.5.
'LineStyle' : '-', '--', '-.', ':'. Default is '-'.
'ShowPoints' : TRUE or FALSE specifying whether to show the points being selected. Default is false.
'ConnectPoints' : TRUE or FALSE specifying whether to connect the points as they are being selected. This only applies when 'ShowPoints' is set to TRUE. Default is true.

[X,Y,BUTTON] = ginputc(...) returns a third result, BUTTON, that contains a vector of integers specifying which mouse button was used (1,2,3 from left) or ASCII numbers if a key on the keyboard was used.

[X,Y,BUTTON,AX] = ginputc(...) returns a fourth result, AX, that contains a vector of axes handles for the data points collected.

[x, y] = ginputc;

[x, y] = ginputc(5, 'Color', 'r', 'LineWidth', 3);

[x, y, button] = ginputc(1, 'LineStyle', ':');

subplot(1, 2, 1); subplot(1, 2, 2);
[x, y, button, ax] = ginputc;

[x, y] = ginputc('ShowPoints', true, 'ConnectPoints', true);

Cite As

jiro (2021). Custom GINPUT (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (41)

Nicolas Pipard

It's worth a 5star because it is much faster to start than ginput.
However for subplot it does not work because it always get gca for the last subplot no matter what.
For ginput, there is no ax output, but then you can get the axe by just referring to gca when doing ginput(1). This is useful when you have a matrix of subplot and you check which member you have clicked. Try the following, and you'll see it works with ginput but not with ginputc:
figure, hAx_mine(1)=subplot(1, 2, 1); hAx_mine(2)=subplot(1, 2, 2);
[x, y, button] = ginput(1)

I have also asked help on stackoverflow:

Siddharth Rawat

John Pisoir

There seems to be a problem with @(x)validatestring if I want to define Linestyle. Doing "ginputc('LineStyle', '--')", I get an error: The value of 'LineStyle' is invalid. It must satisfy the function: @(x)validatestring(x,{'-','--','-.',':'}).

Fidelis Junior

LIU Likun

Super useful thanks! Error when pressing the zoom button, I add the following code from ginput function.
state = uisuspend(hFig);
toolbar = findobj(allchild(hFig),'flat','Type','uitoolbar');
if ~isempty(toolbar)
ptButtons = [uigettool(toolbar,'Plottools.PlottoolsOff'), ...
ptState = get (ptButtons,'Enable');
set (ptButtons,'Enable','off');


if ~isempty(toolbar) && ~isempty(ptButtons)
set (ptButtons(1),'Enable',ptState{1});
set (ptButtons(2),'Enable',ptState{2});

LIU Likun

Thomas Poulard

Eleni Papadatou



Nice implementation but as Markus mentioned, the crosshair is behind the panel (2018a), please fix.

Michael F

Michael Daldini

In my case it is very laggy like the normal version, do you know how can I solve this?

Sebastian Werner


exactly what i'm looking for (image has very dark background & i can't see the cross in ginput)

Salman Naqvi

Exactly what I wanted. And I didn't realize till later that it can even connect points with lines. I further encapsulated deleteLastPoint logic into a separate function. Please see if you can add my suggestion into your code.

NOTE: The only issue is that hitting backspace/delete currently deletes two of the last points instead of just one!?

aimi shaharuddin

Claude Holenstein

exactly what i was looking for

Jiseok Lee

Super useful thanks

natalia kouremeti

Thank you !!!!!

Nik Ritzmann

Nice and handy functionality! I used it to segmentize recorded time signal tracks. Triggering caption of the current coordinate by key press was especially usefull, because it allowed me to encode simple label information into each data point.
While this functionality worked smoothly in Matlab R2014b, I encountered a problem when using it on R2015a: There, each key press resulted in two new points being added with the same coordinate. It seems with R2015a, two key press events get fired upon every single key being hit.

I was able to work around this by replacing line 250 (in version dated 2016-09-01)

if isempty(key)


if isempty(key) || nargin >= 2 && ~strcmp(varargin{2}.EventName, 'KeyPress') % NkR170403: Accept only KeyPress


Hi Thank you fot this is very nice. I have been unabe to add a scroll properlly to this can you help me on this please. kind regards.


Mathworks, implement this please!!!

Gary Pajer

R2015a, macOS 10.12.1.

This function does not work if I have zoomed in on a portion of my axes, and the zoom icon (GUI figure toolbar) is active. If I first de-select the zoom icon, it works. ginput() works with zoom selected, ginputc() does not.

If I forget to de-select the zoom icon, and click in my axes, nothing seems to happen. But if I then de-select zoom, the GUI gets confused. *Other* axes do not draw, and there is a crosshair frozen in the upper left of the GUI, plus another movable crosshair that, does nothing on the first use, but works properly on the second use. The frozen crosshair appears to be "permanent".


Thanks for the program. I found out that it doesn't show properly the values smaller than 0.0001. Can you let me know how can I fix it.

Devraj Mandal

It is very good.


Michel bertrand

This looks like nice piece of work; I just used it for a few hours testing the program I am developing now, but I will definitely try it later on other codes to see how it goes instead of ginput
One thing I liked, is that I could easily modify ginputc so it also records key modifier (alt,control,shift pressed while clicking); I have been waiting for this for a long time until I saw ginputc.
I also observed a rather slow cursor (compared to ginput), and in my case the solution suggested by Nathanael Berglund is not improving this. ( not so surprising since I do not not have so many plot objets)
I am using ginputc in a program run on a remote computer (through remote desktop) which obviously slows down cursor movement; but it is nevertheless uncomfortably slower than ginput in the same situation. Any idea how to improve cursor speed; I could check with the profiler, though I never used the profiler for improving event driven code.

azzam mahmood

thanks for the great tool, but i facing this massage when i tried to use this function
warning: "Warning: Text handle output is not supported for managed labels. "
error: "Error while evaluating uicontrol Callback"

Markus Roßmann

If I use Matlab 2014a this function doesn't work but with Matlab 2011 it works great.
At Matlab 2014a the couror is always behind the panel. That is not usable because you can not see where you click.

Is there a solution available?


is there a way to move the cursor using the cursor keys on the keyboard? I think this would be great since it could be more accurate.

My cursor is quite slow using this function, moves around with maybe 5-10 frames per second.

Nathanael Berglund

One thing I have noticed. If I am using this on a figure that has a large number of plot objects, the call to isprop can be very slow, so I added a line to exclude the objects plotted to the current axis from consideration when setting the event handler properties.

allHG = findall(hFig);

I added:
allHG = allHG(cellfun(@(x) hAx~=x, get(allHG,'Parent'))); % Exclude graphics objects that are children of the current axis

which sped things up quite a bit for my graphics-heavy plot.


just great!


I was too quick with that question. Found the included function for removing points via delete/backspace. Many thanks!


it would be great if one could define a key or mousebutton (like right click) to remove the last point!

John L

Jiro, thanks for the great tool. Is it possible to make it so that the cross-hair is a specified size (i.e. limit the size to a cm or two)? When I use it, it goes across the page outside the panel.

Wolfgang Schwanghart

Jiro, thanks for the quick reply and fix!





Thanks for pointing this out. It was a bug. I'll fix it and resubmit. In the meantime, please replace the following lines in my code:

addParamValue(p, 'LineStyle', '-' , @(x) validatestring(x, ...
{'-', '--', '-.', ':'}));


addParamValue(p, 'LineStyle', '-' , @(x) ischar(validatestring(x, ...
{'-', '--', '-.', ':'})));

Wolfgang Schwanghart

Great tool! Yet, there seems to be a bug which I cannot resolve. I called ginputc with

[x,y,button] = ginputc(1,'color','r','LineStyle','--')

in a function mytool. Despite the LineStyle should be valid, I receive following error message.

Error using mytool>ginputc (line 135)
Argument 'LineStyle' failed validation @(x)validatestring(x,{'-','--','-.',':'}).
Error in mytool (line 29)
[x,y,button] = ginputc(1,'color','r','LineStyle','--');

Any ideas?



Can you elaborate a little bit more? Are you saying that you want to pass a "list" of x-y coordinates, so that the additional points you click will be appended to the list? Or are you saying that you want to pass a pair of x-y coordinates and the cursor to be initially placed at that coordinate?


one thing: it would be nice to call the function with already a list of [x,y] coordinates. I'm trying to implement it, but it doesn't work properly yet...


Great function. Exact what I needed.
And with a clear code too :)

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

Inspired: gdist, clickz, GINPUT FOR UIAXES, gdistm

Community Treasure Hunt

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

Start Hunting!