azim - here is a quick and rough way to do what I think that you want. It makes several assumptions which are hopefully valid for what you are trying to do, namely that plot (or some equivalent) function is used to plot the data on the 2D axe. I've used GUIDE to mock up a GUI - it has a single axes (named axes1), a push button (named pushbutton1) to plot some data on the axes, and another button (pushbutton2) to delete the selected graphics object. The first button callback is
function pushbutton1_Callback(hObject, eventdata, handles)
colours = {'b','r','g','k','m','y'};
if ~isfield(handles,'plotHandles');
handles.plotHandles = [];
end
numHandles = mod(length(handles.plotHandles),6);
x = -2*pi:0.05:2*pi;
y = sin(x) + numHandles;
h = plot(handles.axes1,x,y,colours{numHandles+1});
handles.plotHandles = [handles.plotHandles ; h];
guidata(hObject,handles);
The above just plots up to six sine waves on the axes. The important part is the saving of the plot graphics handle to the handles structure. This will allow us to delete the wave later on.
Now, in order to enable the user to delete a graphic object on the axes, we are going to assign a callback to the GUI/figure. In the Opening function (myGuiName_OpeningFcn), add the following code
set(hObject, ...
'WindowButtonDownFcn', @mouseDownCallback);
hObject is our GUI/figure, and we are going to create a callback for the mouse button down event. This callback is defined as
function mouseDownCallback(figHandle,varargin)
DISTANCE_THRESHOLD = 2;
handles = guidata(figHandle);
currentPoint = get(figHandle, 'CurrentPoint');
x = currentPoint(1,1);
y = currentPoint(1,2);
axesPos = get(handles.axes1,'Position');
minx = axesPos(1);
miny = axesPos(2);
maxx = minx + axesPos(3);
maxy = miny + axesPos(4);
if x>=minx && x<=maxx && y>=miny && y<=maxy
if isfield(handles,'plotHandles')
currentPoint = get(handles.axes1, 'CurrentPoint');
x = currentPoint(2,1);
y = currentPoint(2,2);
minDist = Inf;
minHndlIdx = 0;
for k=1:length(handles.plotHandles)
xData = get(handles.plotHandles(k),'XData');
yData = get(handles.plotHandles(k),'YData');
dist = min((xData-x).^2+(yData-y).^2);
if dist<minDist && dist<DISTANCE_THRESHOLD
minHndlIdx = k;
minDist = dist;
end
end
if minHndlIdx~=0
handles.graphicToDeleteHandleIdx = minHndlIdx;
else
handles.graphicToDeleteHandleIdx = [];
end
for k=1:length(handles.plotHandles)
if k==minHndlIdx
set(handles.plotHandles(k),'LineStyle',':');
else
set(handles.plotHandles(k),'LineStyle','-');
end
end
guidata(figHandle,handles);
end
end
The above is fairly straightforward - we take the position where the user has pressed within the axes and try to find the closest graphics object to this position. If found, then se save its index within the plotHandles array to the handles object so that we can access this index within the delete button callback which is defined as
function pushbutton2_Callback(hObject, eventdata, handles)
if isfield(handles,'graphicToDeleteHandleIdx') && ~isempty(handles.graphicToDeleteHandleIdx)
delete(handles.plotHandles(handles.graphicToDeleteHandleIdx));
handles.plotHandles(handles.graphicToDeleteHandleIdx) = [];
handles.graphicToDeleteHandleIdx = [];
guidata(hObject,handles);
end
I've attached the code for this example. Hope it helps!