File Exchange

image thumbnail

Graphical data selection tool

version 1.0.0.0 (10.2 KB) by John D'Errico
A graphical tool to modify your plots and locate data points

28 Downloads

Updated 20 Feb 2007

View License

Editor's Note: This file was a File Exchange Pick of the Week

Selectdata is a tool that allows the user to graphically select one or more points from a 2-d plot using a variety of graphical tools. You can specify a lasso tool, a paint brush of two shapes and various sizes, an expanding rectangle, or just click near a desired point.

As points are selected with the tool of choice, they become highlighted (temporarily) until the mouse button is released. Then the index of the points selected, plus their (x,y) coordinates are returned. The user has control of many aspects of the tool, including whether the selected points are to be deleted from the plot. (There are also some hidden options that I did not include in the help itself. You can find them by editing the file.)

I wrote this tool as a generic utility one could call from within a gui based modeling application. It could also be useful when called directly from the commandline to identify data points or sets of points on a plot, or to delete points (outliers) from a plot. It can be used to return the points selected or all of those that remained unselected. In my own use of this tool, I use it to identify sets of points for which I wish to modify the regression weights.

Some examples of use:

Simple selection with a lasso - I selected one point from one of the two curves, and two points from the second curve.

plot(rand(5,2),rand(5,2),'o')
[pind,xs,ys] = selectdata('selectionmode','lasso')
pind =
[ 3]
[2x1 double]
xs =
[ 0.099422]
[2x1 double]
ys =
[ 0.87571]
[2x1 double]

Selection with a circular brush, deleting those selected from the plot.

plot(rand(5,2),rand(5,2),'o')
[pind,xs,ys] = selectdata('sel','br','action','delete')
pind =
[3]
[4]
xs =
[0.67745]
[0.69115]
ys =
[0.76837]
[0.59464]

Plot two curves, one is data, the second a model. Allow the selection tool (the lasso) to only see the data.

x = 0:.01:1;
y = exp(x);
ydata = y + randn(size(y))/2;

h1 = plot(x,y,'r-');
hold on
h2 = plot(x,ydata,'o');
[pind,xs,ys] = selectdata('sel','lasso','ignore',h1)

pind =
20
78
99
xs =
0.19
0.77
0.98
ys =
2.3639
3.2279
3.7707

Thanks to Jiro and a reviewer for some great ideas for the enhancements implemented in version 2.

Version 3 has the ability to label each selected point with its coordinates.

Please send me e-mail if you find any problems.

Cite As

John D'Errico (2021). Graphical data selection tool (https://www.mathworks.com/matlabcentral/fileexchange/13857-graphical-data-selection-tool), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (67)

Mike Piva

Furious D

I use this frequently and love it, thanks John! Still works in 9.8.0.1323502 (R2020a).

There is a bug in your software when selecting points on log plots (i.e., when XScale is 'Log' or YScale is 'Log'). When sending the points to inpolygon, you need to map appropriately onto log-space. There is a simple fix, however. If you modify the subfunction testpoly as follows, it checks the current axes for log scales and passes the points to an appropriate wrapper which does this automatically.

function [pl,xsel,ysel,nsel] = testpoly(xv,yv,xdata,ydata)
% checks which points are inside the given polygon

%% D. Habersat Nov 05, 2020
logx = strcmpi(get(gca,'XScale'),'log');
logy = strcmpi(get(gca,'YScale'),'log');
if logx && logy
inpoly_fun = @inpolygon_loglog;
elseif logx
inpoly_fun = @inpolygon_semilogx;
elseif logy
inpoly_fun = @inpolygon_semilogy;
else
inpoly_fun = @inpolygon;
end
%%
% was there more than one set of points found in the plot?
if ~iscell(xdata)
% only one set, so xdata and ydata are not cell arrays

% Which points from the data fall in the selection polygon?
pl = find(inpoly_fun(xdata,ydata,xv,yv)); % D. Habersat Nov 05, 2020
nsel = length(pl);

xsel = xdata(pl);
ysel = ydata(pl);
else
% it was a cell array, so there were multiple sets.
pl = cell(size(xdata));
xsel = pl;
ysel = pl;
nsel = 0;
for i = 1:numel(xdata);
pl{i} = find(inpoly_fun(xdata{i},ydata{i},xv,yv)); % D. Habersat Nov 05, 2020
nsel = nsel + length(pl{i});

if ~isempty(pl{i})
xsel{i} = xdata{i}(pl{i});
ysel{i} = ydata{i}(pl{i});
end

end
end
%% D. Habersat Nov 05, 2020
function [in,on] = inpolygon_semilogx(xdata,ydata,xv,yv)
[in,on] = inpolygon(log(xdata),ydata,log(xv),yv);
end
function [in,on] = inpolygon_semilogy(xdata,ydata,xv,yv)
[in,on] = inpolygon((xdata),log(ydata),(xv),log(yv));
end
function [in,on] = inpolygon_loglog(xdata,ydata,xv,yv)
[in,on] = inpolygon(log(xdata),log(ydata),log(xv),log(yv));
end
%%
end % subfunction end

Qing Wang

Hi John! This seems to be what I need, but somehow it does not work with my version of MATLAB (9.7.0.1296695 (R2019b) Update 4). Do you have an updated version?

Thank you very much.

Samir Charif

Very good tool.
Works well and is easy to use.

Aurea94

Thomas Jacobsen

Is there any way to keep the ROI on the plot to visually see what data points were excluded from the outputted data?

Angga Perima

Thanks!!!

NB : my first time comment after 6 years in this community.

Guy Burke

Ha_Ka

Claudiu Papasteri

Austin Fite

John,

Thank you for your submission, this is a much-needed tool that you executed as well or better than any built-in matlab tool. However, I ran into a few issues when integrating with my GUI that you may want to incorporate:

<line 298>
fighandle = get(params.Axes, 'parent');
should really be
fighandle = ancestor(params.Axes,'figure');

The second is more of a functionality change, but something to consider: I have my lineobjects parented to hgtransforms which creates errors when you look for XData in the axes children. You provide the option to ignore the hgtransforms, but I have no way to target the children of the hgtransform objects. I think a more robust way to handle this is to allow the user to select the specific handles to include--not ignore.

Thanks again for your hard work! This is an excellent utility and the small issues I've had were not hard to fix.

Valeriy

Hi John,

This tool is masterpiece! Great work!
It's what I needed!

Best regards, Valeriy

Jesper Kamp Jensen

Hi John,
Thanks for the nice tool!
Sometimes I get an error when I use it:
"Error using axis (line 80)
x-axis and y-axis limits have different data types. Use xlim and ylim functions instead.

Error in selectdata (line 286)
axissize = axis;"

Do you know what the issue is?

Best, Jesper

siti radzi

Hi John, I want to select and remove multiple points.
Currently it only stores one point at one click and finish.
I hope it can hold until done clicking all multiple points that we want.

Hope you can help me

Thanks a lot

Francois Aube

Zhengyi

A high-quality submission. The script is fast and elegant.

Philippe Marchal

AB

Hi! Can it work for a 3d scatter plot? If you can add that functionality, it will be great

Carl Witthoft

Wonderful work on both the documentation and the flexibility provided via the extensive varargin list. The design style of this code should be used as a standard for coders to follow!

Gabriel Ruiz-Martinez

Jacob Everidge

Jacob Everidge

Awesome tool!

Have you figured out how to implement it on guide?

Fernando Sarabia

Great tool!

Fernando Sarabia

Ferit Akgul

Superb tool!

Alejandro Astorga

Really Usefull

Jessica Hiscocks

Found this useful, but didn't realise at first mouse button must be held down when using brush setting. Still saved me a lot of time, thanks for putting this up.

Sebastiano Vascon

nice work but...could please provide a version that works with gscatter and scatter ?

It would be very useful :)

Thanks !

Tarkes Dora

I was trying this on saved Scatter figure file. I am unable to apply this on a scatter plot.One would be interested in selecting points from a scattered cloud of points. Such tools has most important application in these kind of problems.

Tyler

Vikram Goel

Jeremias Sibold

A wonderful tool.

if you have a Text field in your data plot than this tool crashes.

to fix this you should only get the xdata and ydata from graphic types of 'line'.

So to fix this you should implement this short code at line 314

% strip out xdata and ydata of lines
Types=get(hc, 'Type');
Linetypes=zeros(1,numel(Types));
for k=1:numel(Types)
if strcmp(Types{k},'line')
Linetypes(k)=true;
end
end
xdata = get(hc(find(Linetypes)),'xdata');
ydata = get(hc(find(Linetypes),'ydata');

Joseph

Any wa to make multiple selections?

Aurelien

A wonderful tool, and nicely coded!

Ghassan Falouji

Thanks a LOT

Emily Toomey

Fantastic tool! So helpful and easy to use. Exactly what I've been looking for.

Fabricio Fernandez

Anders Almholt

Great Tool.

I would like to implement the selection tool in a GUI. But I get an error message that the figure handle is in use by another object.

Any ideas?

Architha Kommera

Hi, this tool is very helpful but this cant work on 3D plots. I'm looking for the exact same thing for 3D plots. can somebody suggest relevant function?

Pietro

Orly amsalem

Mark S.

Really a great function. I cannot believe that Matlab did not already include this,especially since the curvefit GUI basically has this sort of thing built in. It saved me tons of time.

Renee

Easy to use.

suggestion: add ability to choose multiple single points but easy enough to code as a while loop, e.g.:
% initialize selection variables
i = 0;
choose = 'y';

while strmatch('y', choose)
i = i + 1; % increment peak count
[j, x(i), y(i)] = selectData('selectionmode', 'closest'); % user script
choose = input([num2str(i) ' peaks chosen -- choose another? (y or n) '], 's');
end

Ricardo Portal

Very helpful. Thanks

arnold

Great function, I wish it worked with images though. Been looking for a function that provides me with a way to select a certain image area and it gives me the list of indices.

Is there a way to do this with your selection tool?

Sergey Kucheryavskiy

Giacomo

ori

nice tool...

Christine

salamay

Ramiro Massol

hi John, it would be nice to add some 'disable' functionality to your selectdata function. By this I mean the ability to launch selectdata under control of a togglebutton (similar to the brush togglebutton), so if the user wants to cancel selection he can also toggle off the controller.
best
ps: by the way, there's a minor bug in the 'axes' property. If you have more than 1 axes in the figure and the user tries to select something from the wrong axes (i tried so far 'rect' as a tool) selectdata allows other axes to be used for selection though the selection tools does not work properly.

John D'Errico

Hmm. Having just now installed R2010a, I'll do my best to make it compliant immediately. This code has always been slightly sensitive, so I'm not surprised if a newer release might have caused problems.

Ramiro Massol

this function does not work in Matlab 2009a. I always get the following error:

??? Error using ==> get
Invalid property found.
Object Name : text
Property Name : 'xdata'.
Error in ==> selectdata at 315
xdata = get(hc,'xdata');

any clues?

M

N-I-C-E.
I cannot understand why something similar is not implemented in matlab yet.
It would be nice if one could add several selections by pressing e. g. "Ctrl".

Arthur

Joshua

Cheng-ta Yang

Malcolm Lidierth

Simple-to-use and attractive tool.
One trivial suggestion:
Line 198
fighandle = get(params.Axes,'parent');
can be replaced with
fighandle=ancestor(params.Axes,'figure');
which extends selectdata's use to axes parented by e.g. a uipanel.

adhi dm

two thumbs up!!

Stefan Novak

Just what I was looking for!

One adjustment had to be made. Lines 645-646 had to be changed from:

xselect = xdata(pointslist);
yselect = ydata(pointslist);

To:

xselect = xdata(pointslist<=size(xdata,1));
yselect = ydata(pointslist<=size(ydata,1));

Otherwise elements in pointslist was trying to call on elements outside of the range of x/y data.

Hope this helps.

G Coppola

Exactly what I needed

Thanks

F Moisy

just perfect

Marcel Bison

Amazing job !!
Very usefull file
Thanks

JJ nigaud

this script rocks... hail!!!

Zhijun Wang

Excellent work and very useful file!

jun ziyang

Great work!

nice job

Interesting tool! It seems that if one is not satistified with current selection, the user has to re-run the command. It would be more handy if the user can keep trying until s/he satisfy the results.

MATLAB Release Compatibility
Created with R2006b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!