MATLAB: Do the callbacks only work if triggered in the order they are defined

callbackguideMATLAB

I have defined my callbacks like so:
set(handle,'callback',{@foo, handles})
function foo(hObject, event_data,handles)
...
set(handle, callback',{@bar, handles})
function bar(hObject, event_data,handles)
...

Best Answer

This issue is because each callback is being set the end of the body of the previous callback. Thus, they will not be set unless the previous callback is executed. The solution is to move all the 'set' function calls to above all the callback definitions:
set(handle,'callback',{@foo, handles})
set(handle,'callback',{@bar, handles})
function foo(hObject, event_data,handles)
...
end
function bar(hObject, event_data,handles)
...
end