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

Note

When you call a plotting function, such as plot or 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.

Window Callbacks

Figures have additional properties that execute callbacks with specific user actions. These additional properties are not available in MATLAB Online™.

  • CloseRequestFcn — Executes when a request is made to close the figure (by a close command, by the window manager menu, or by quitting MATLAB ).

  • KeyPressFcn — Executes when you press a key while the cursor is in the figure window.

  • ResizeFcn — 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.