Main Content

Class Methods for Graphics Callbacks

Referencing the Method

To use an ordinary class method as callback for a graphics object, specify the callback property as a function handle referencing the method. For example,


Where your class defines a method called sliderCallback and obj is an instance of your class.

To use a static method as a callback, specify the callback property as a function handle that includes the class name that is required to refer to a static method:


Syntax for Method Callbacks

For ordinary methods, use dot notation to pass an instance of the class defining the callback as the first argument:


Define the callback method with the following input arguments:

  • An instance of the defining class as the first argument

  • The event source handle

  • The event data

The function signature would be of this form:

function methodName(obj,src,eventData)

For static methods, the required class name ensures MATLAB® dispatches to the method of the specified class:


Define the static callback method with two input arguments — the event source handle and the event data

The function signature would be of this form:

function methodName(src,eventData)

Passing Extra Arguments

If you want to pass arguments to your callback in addition to the source and event data arguments passed by MATLAB, you can use an anonymous function. The basic syntax for an anonymous function that you assign to the graphic object's Callback property includes the object as the first argument:


The function signature would be of this form:

function methodName(obj,src,eventData,varargin)

Use a Class Method for a Slider Callback

This example shows how to use a method of your class as a callback for an uicontrol slider.

The SeaLevelSlider class creates a slider that varies the color limits of an indexed image to give the illusion of varying the sea level.

Class Definition

Define SeaLevelSlider as a handle class with the following members:

  • The class properties store figure and axes handles and the calculated color limits.

  • The class constructor creates the graphics objects and assigns the slider callback.

  • The callback function for the slider accepts the three required arguments — a class instance, the handle of the event source, and the event data. The event data argument is empty and not used.

  • The uicontrol callback uses dot notation to reference the callback method: ...'Callback',@obj.sliderCallback.

classdef SeaLevelSlider < handle
      function obj = SeaLevelSlider(x,map)
         obj.Figure = figure('Colormap',map,...
         obj.Axes = axes('DataAspectRatio',[1,1,1],...
         obj.CLimit = get(obj.Axes,'CLim');
      function sliderCallback(obj,src,~)
         minVal = get(src,'Value');
         maxVal = obj.CLimit(2);
         obj.Axes.CLim = [minVal maxVal];

Using the SeaLevelAdjuster Class

The class uses the cape image that is included with the MATLAB product. To obtain the image data, use the load command:

load cape X map

After loading the data, create a SeaLevelSlider object for the image:

slaObj = SeaLevelSlider(X,map);

Move the slider to change the color mapping and visualize a rise in sea level.

Image of coastline with slider

Related Topics