MATLAB: Is the ‘ButtonDownFcn’ property of an axes in a GUI not called after I plot a figure in MATLAB 7.3 (R2006b)

axesbuttondownfcnMATLABproperties

The 'ButtonDownFcn' property of an axes is defined in the property inspector during the GUI building process.
However, this function is not being called when I run my GUI.

Best Answer

High-level plotting functions, by default, clear the axes properties (except the 'position' and the 'units' property).
To prevent calls to PLOT, BAR, etc. from replacing the buttondownfcn, set the 'NextPlot' property of the axes to 'replacechildren' instead of the default 'replace'. This will clear the axes contents and plot the new data without replacing the axes object itself.
If you want to plot data over your existing data, then set the 'NextPlot' property of the axes to 'Add'.
An alternative workaround is to assign 'ButtonDownFcn' for the axes again after calling the plot.
function test()
% Creating dummy data 'a' and 'b'
a=3;
b=4;
h=figure;
plot(rand(3));
axes_handle = gca;
set(axes_handle, 'ButtonDownFcn', {@FcnName,a,b});
function FcnName(src,evnt,a,b)
disp('click down!!!')
disp(a)
disp(b)