Code covered by the BSD License  

Highlights from
Custom GINPUT

4.71429
4.7 | 8 ratings Rate this file 88 Downloads (last 30 days) File Size: 4.56 KB File ID: #38703 Version: 1.0
image thumbnail

Custom GINPUT

by

Jiro Doke (view profile)

 

GINPUT with customizable cursor.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

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.

Examples:
 [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);

Acknowledgements

This file inspired Gdistm, Gdist, and Clickz.

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Other requirements Requires R2007b or newer.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (16)
05 Jul 2015 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.
Thanks

18 Jun 2015 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"

10 Jun 2015 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?

Comment only
25 May 2015 arnold

arnold (view profile)

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.

Comment only
26 Jan 2015 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.

After:
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.

Comment only
12 Sep 2014 adi

adi (view profile)

just great!

28 Jul 2014 arnold

arnold (view profile)

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

28 Jul 2014 arnold

arnold (view profile)

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

10 Mar 2014 John L

John L (view profile)

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.

Comment only
24 Jan 2013 Wolfgang Schwanghart

Jiro, thanks for the quick reply and fix!

23 Jan 2013 Anon

Anon (view profile)

Thanks!

23 Jan 2013 Jiro Doke

Jiro Doke (view profile)

@Wolfgang,

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, ...
{'-', '--', '-.', ':'}));

to:

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

Comment only
23 Jan 2013 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?

Comment only
28 Nov 2012 Jiro Doke

Jiro Doke (view profile)

@Stefano,

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?

Comment only
13 Nov 2012 Stefano

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...

Comment only
13 Nov 2012 Stefano

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

Contact us