Callbacks — Programmed Response to User Action
What Are Callbacks?
A callback is a function that executes in response to some predefined user action, such as clicking on a graphics object or closing a figure window. Associate a callback with a specific user action by assigning a function to the callback property for that user action.
All graphics objects have the following properties for which you can define callback functions:
ButtonDownFcn— Executes when you press the left mouse button while the cursor is over the object or is within a few pixels of the object.
CreateFcn— Executes during object creation after MATLAB® set all properties
DeleteFcn— Executes just before MATLAB deletes the object
When you call a plotting function, such as
bar, MATLAB creates
new graphics objects and resets most figure and axes properties. Therefore,
callback functions that you have defined for graphics objects can
be removed by MATLAB. To avoid this problem, see Define a Callback as a Default.
Figures have additional properties that execute callbacks with specific user actions. These additional properties are not available in MATLAB Online™.
KeyPressFcn— Executes when you press a key while the cursor is in the figure window.
SizeChangedFcn— Executes when you resize the figure window.
WindowButtonDownFcn— Executes when you press a mouse button while the cursor is over the figure background, a disabled user-interface control, or the axes background.
WindowButtonMotionFcn— Executes when you move the cursor in the figure window (but not over menus or title bar).
WindowButtonUpFcn— Executes when you release the mouse button, after having pressed the mouse button in the figure.
Callbacks in Live Editor
You can define and execute a callback for a figure in the Live Editor with one of these techniques:
Create a figure callback and pass source and event data as parameters in the callback.
Create a figure callback and do not pass source or event data as a parameter in the callback.
Create a callback that includes a function for identifying a graphics object, such as
Keyboard-based callback properties and anonymous function callbacks using
Figure objects from the MATLAB workspace, such as
fig = figure;, are not currently
supported in the Live Editor.
For more information about callback properties, see Figure Properties.