MATLAB: Addlistener question…

addlistener listener function input arguments handle

I have created a listener which calls 'myfunction' whenever the limits of a certain set of axes changes:
addlistener(handles.axes1,'XLim', 'PostSet', @myfunction);
function myfunction
...
This works fine. Now I want to pass the handles structure to myfunction:
addlistener(handles.axes1,'XLim', 'PostSet',{@myfunction,handles});
function myfunction(handles)
...
However I recieve the error:
callbacks need to be of type function handle
I cannot pass handles, or any additional arguments to myfunction in this case. Why is this so? How would I set up a listener object properly?

Best Answer

The posted example does not work on my computer under Matlab 2011b:
addlistener(handles.axes1,'XLim', 'PostSet', @myfunction);
function myfunction % ERROR
Calling the callback function fails, because it needs at least 2 inputs:
function myfunction(ObjH, EventData) % OK
If you need a third input:
addlistener(handles.axes1,'XLim', 'PostSet', ...
@(ObjH, EventData) myfunction(ObjH, EventData, handles));
function myfunction(ObjH, EventData, handles)