The Wayback Machine - https://web.archive.org/web/20110607164555/http://www.mathworks.com:80/help/techdoc/matlab_oop/f1-5978.html
Skip to Main Content Skip to Search
Product Documentation

Class Methods for Graphics Callbacks

Callback Arguments

You can use class methods as callbacks for Handle Graphics objects by specifying the callback as an anonymous function. Anonymous functions enable you to pass the arguments required by methods (i.e., the first argument is a class object) and graphics object callbacks (i.e., the event source and the event data), as well as any other arguments you want to pass to the function.

The following links provide general information on graphics object callbacks and anonymous functions.

Background Information

General Syntax for Callbacks

The basic syntax for a function handle that you assign to the graphic object's Callback property includes the object as the first argument:

@(src,event)method_name(object,src,event,additional_arg,...)

You must define the callback method with the following signature:

method_name(object,src,event)

Object Scope and Anonymous Functions

Anonymous functions take a snapshot of the argument values when you define the function handle. You must, therefore, consider this scoping when assigning the Callback property. The following two sections provide examples.

Using Value Classes

Consider the following snippet of a value class definition:

classdef SeaLevelAdjuster
   properties
      Slider
   end
   methods
      function seal = SeaLevelAdjuster
         ...
        seal.Slider = uicontrol('Style','slider');
        set(seal.Slider,'Callback',@(src,event)slider_cb(seal,src,event)
       end
   end
end

This class assigns the Callback property in a separate set statement so that the value object's (seal) Slider property has been defined when you create the function handle. Otherwise, Handle Graphics freezes seal before the uicontrol's handle is assigned to the Slider property.

Using Handle Classes

The difference in behavior between a handle object and a value object is important in this case. If you defined the class as a handle class, the object is a reference to the underlying data. Therefore, when the MATLAB runtime resolves the function handle, the contents of the object reflects assignments made after the function handle is defined:

classdef SeaLevelAdjuster < handle
   ...
   properties
      Slider
   end
   methods
      function seal = SeaLevelAdjuster
         ...
         seal.Slider = uicontrol('Style','slider',...
         'Callback',@(src,event)slider_cb(seal,src,event));
      end
   end
end

Example — Class Method as a Slider Callback

This example defines a slider that varies the color limits of an indexed image to give the illusion of varying the sea level.

Displaying the Class Files

Open the SeaLevelAdjuster class definition file in the MATLAB editor.

To use the class, create a folder named @SeaLevelAdjuster and save SeaLevelAdjuster.m to this folder. The parent folder of @SeaLevelAdjuster must be on the MATLAB path.

Class Properties

The class defines properties to store graphics object handles and the calculated color limits:

classdef SeaLevelAdjuster < handle
   properties
      Figure = [];
      Axes = [];
      Image = [];
      CLimit = [];
      Slider = [];
   end
end

Class Constructor

The class constructor creates the graphics objects and assigns the slider callback (last line in code snippet):

methods
   function seal = SeaLevelAdjuster(x,map)
   seal.Figure = figure('Colormap',map,...
      'Resize','off',...
      'Position',[100 100 560 580]);
   seal.Axes = axes('DataAspectRatio',[1 1 1],...
      'XLimMode','manual',...
      'YLimMode','manual',...
      'DrawMode','fast',...
      'Parent',seal.Figure);
   seal.Image = image(x,'CDataMapping','scaled','Parent',seal.Axes);
   seal.CLimit = get(seal.Axes,'CLim');
   seal.Slider = uicontrol('Style','slider',...
      'Parent',seal.Figure,...
      'Max',seal.CLimit(2),...
      'Min',seal.CLimit(1)-1,...
      'Value',seal.CLimit(1),...
      'Units','normalized',...
      'Position',[.9286 .1724 .0357 .6897],...
      'SliderStep',[.005 .002],...               
      'Callback',@(src,event)slider_cb(seal));
   end % SeaLevelAdjuster
end % methods

The callback function for the slider is defined to accept the three required arguments — a class instance, the handle of the event source, and the event data:

methods
   function slider_cb(seal)
      min_val = get(seal.Slider,'Value');
      max_val = max(max(get(seal.Image,'CData')));
      set(seal.Axes,'CLim',[min_val max_val])
      drawnow
   end % slider_cb
end % methods

Using the SeaLevelAdjuster Class

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

load cape

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

seal = SeaLevelAdjuster(X,map)

Move the slider to change the apparent sea level and visualize what would happen to Cape Cod if the sea level were to rise.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A; sessions led by MATLAB experts.

 © 1984-2011- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS