How can I select a graph by mouse clicking and delete it?

8 views (last 30 days)
I have five graphs in an axes in a GUI. How can I select one of the them by mouse clicking and delete it? Thanks

Accepted Answer

Geoff Hayes
Geoff Hayes on 23 Dec 2015
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)
% define an array of colours
colours = {'b','r','g','k','m','y'};
% is the plot handles field defined for the handles structure?
if ~isfield(handles,'plotHandles');
handles.plotHandles = [];
end
% determine the number of handles (a maximum of six are plotted)
numHandles = mod(length(handles.plotHandles),6);
% get the x and y data for the sine wave, offset by the number of handles
x = -2*pi:0.05:2*pi;
y = sin(x) + numHandles;
% plot the data
h = plot(handles.axes1,x,y,colours{numHandles+1});
% update the plot handles to the array of plot handles
handles.plotHandles = [handles.plotHandles ; h];
% save the data
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)
% maximum distance allowed to consider a graphics object selected (arbitrary)
DISTANCE_THRESHOLD = 2;
% get the handles structure
handles = guidata(figHandle);
% get the position where the mouse button was pressed (not released)
% within the GUI
currentPoint = get(figHandle, 'CurrentPoint');
x = currentPoint(1,1);
y = currentPoint(1,2);
% get the position of the axes within the GUI
axesPos = get(handles.axes1,'Position');
minx = axesPos(1);
miny = axesPos(2);
maxx = minx + axesPos(3);
maxy = miny + axesPos(4);
% is the mouse down event within the axes?
if x>=minx && x<=maxx && y>=miny && y<=maxy
% do we have graphics objects?
if isfield(handles,'plotHandles')
% get the position of the mouse down event within the axes
currentPoint = get(handles.axes1, 'CurrentPoint');
x = currentPoint(2,1);
y = currentPoint(2,2);
% we are going to use the x and y data for each graphic object
% and determine which one is closest to the mouse down event
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 we have a graphics handle that is close to the mouse down
% event/position, then save its index into the plotHandles array
if minHndlIdx~=0
handles.graphicToDeleteHandleIdx = minHndlIdx;
else
handles.graphicToDeleteHandleIdx = [];
end
% change the line style of the selected object
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 the graphics object
delete(handles.plotHandles(handles.graphicToDeleteHandleIdx));
% remove the graphics object from the array of plot handles
handles.plotHandles(handles.graphicToDeleteHandleIdx) = [];
% clear the handle
handles.graphicToDeleteHandleIdx = [];
% save the data
guidata(hObject,handles);
end
I've attached the code for this example. Hope it helps!
  2 Comments
azim
azim on 24 Dec 2015
Dear Geoff Hayes, Thank you for your complete response. It was very helpful. Regards
Geoff Hayes
Geoff Hayes on 30 Dec 2015
azim's answer moved here
Dear Geoff Hayes, Thank you for your complete response. It was very helpful. Regards

Sign in to comment.

More Answers (1)

Nick Counts
Nick Counts on 1 Nov 2016
If you need to do it programmatically, Geoff seems to have nailed it.
If you are using any of Matlab's built-in toolbar items on your figure, then you actually have the controls built into your GUI already! Either:
  1. Just select the "Edit Plot" tool (which looks like a regular mouse cursor)Then you can either regular-click on an item and press delete (maybe Backspace for a Windows user) or
  2. Right-click on a graphics object and select "Delete" from the context menu that pops up!

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!