Clear Filters
Clear Filters

Trouble with ButtonDownFcn and Hittest in GUIDE

10 views (last 30 days)
Hi All,
I'm making my first GUI and am having trouble with the ButtonDownFcn for a set of axes. I've read that others have had trouble with a similar situation and that the solution was to set the Hittest on all the objects on top of the axes to 'off.' I'm not sure what I'm doing wrong. I've made a cartoon version of my code that recreates the same problem without all the extra functions of my actual GUI. In my code I plot some data "ThePlot" on my axes "TheAxes" and once I do the ButtonDownFcn ceases to work. If I comment out the plot the ButtonDownFcn does work (in this instance it just changes some text). Thanks in advance for the help. The code is as follows:
% --- Executes just before FigureWindow is made visible.
function FigureWindow_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figureff
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to FigureWindow (see VARARGIN)
xvalues = linspace(0,10,1000);
yvalues = sin(xvalues) + sqrt(xvalues);
TheAxes = handles.TheAxes;
ThePlot = plot(TheAxes,xvalues,yvalues,'tag','ThePlotTag');
% Choose default command line output for FigureWindow
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes FigureWindow wait for user response (see UIRESUME)
% uiwait(handles.FigureWindow);
% --- Executes on mouse press over theaxes background.
function TheAxes_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to TheAxes (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
TheText = handles.TheText;
set(TheText,'String','You clicked on the Axes');

Accepted Answer

Jonas Reber
Jonas Reber on 3 Jun 2011
this looks reasonable to me. have you tried
ThePlot = plot(xvalues, yvalues, 'Parent', handles.TheAxes, 'Tag', 'ThePlotTag', 'HitTest', 'off', 'ButtonDownFcn', '');
? or you might want to use a simple line instead of the high-level function plot:
ThePlot = line('Parent', handles.TheAxes, 'XData', xvalues, 'YData', yvalues);
  1 Comment
Walter Roberson
Walter Roberson on 3 Jun 2011
The shorter version of that last line is
ThePlot = line(handles.TheAxes, xvalues, yvalues);
Jonas's version is correct; this version is an abbreviation.

Sign in to comment.

More Answers (1)

Michael on 3 Jun 2011
Thank you Jonas and Walter. Using 'line' is definitely the right way to go. I really appreciate your help!


Find more on Interactive Control and Callbacks 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!