Main Content

Button Down Callback Function

When to Use a Button Down Callback

Button down callbacks execute when users left-click on the graphics object for which the callback is assigned. Button down callbacks provide a simple way for users to interact with an object without requiring you to program additional user-interface objects, like push buttons or popup menu.

Program a button down callback when you want users to be able to:

  • Perform a single operation on a graphics object by left-clicking

  • Select among different operations performed on a graphics object using modifier keys in conjunction with a left-click

How to Define a Button Down Callback

  • Create the callback function that MATLAB® executes when users left-click on the graphics object.

  • Assign a function handle that references the callback function to the ButtonDownFcn property of the object.

    ...'ButtonDownFcn',@callbackFcn

Define the Callback Function

In this example, the callback function is called lineCallback. When you assign the function handle to the ButtonDownFcn property, this function must be on the MATLAB path.

Values used in the callback function include:

  • src — The handle to the line object that the user clicks. MATLAB passes this handle.

  • src.Color — The line object Color property.

function lineCallback(src,~)
   src.Color = rand(1,3);
end

Using the Callback

Here is a call to the plot function that creates line graphs and defines a button down callback for each line created.

plot(rand(1,5),'ButtonDownFcn',@lineCallback)

To use the callback, create the plot and left-click on a line.