Callbacks — Programmed Response to User Action
What Are Callbacks?
Acallbackis 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
——当你按下左键wh执行ile the cursor is over the object or is within a few pixels of the object.CreateFcn
— Executes during object creation after MATLAB®set all propertiesDeleteFcn
— Executes just before MATLAB deletes the object
Note
When you call a plotting function, such asplot
orbar
, 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, seeDefine a Callback as a Default.
Window Callbacks
Figures have additional properties that execute callbacks with specific user actions. These additional properties are not available inMATLAB Online™.
CloseRequestFcn
— Executes when a request is made to close the figure (by aclose
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.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
gca
orfindobj
.
Keyboard-based callback properties and anonymous function callbacks usingFigure
objects from the MATLAB workspace, such asfig
infig = figure;
, are not currently supported in the Live Editor.
For more information about callback properties, seeFigure Properties.