| MATLAB® | ![]() |
| On this page… |
|---|
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.
Function Handle Callbacks — Information on graphics object callbacks
Anonymous Functions — Information about using anonymous functions
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)
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.
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.
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
This example defines a slider that varies the color limits of an indexed image to give the illusion of varying the sea level.
Open the SeaLevelAdjuster class definition file in the MATLAB editor.
To use the class, create a directory named @SeaLevelAdjuster and save SeaLevelAdjuster.m to this directory. The parent directory of @SeaLevelAdjuster must be on the MATLAB path.
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
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,'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),... 'Position',[520 100 20 400],... 'SliderStep',[.005 .002],... 'Callback',@(src,event)slider_cb(seal,src,event)); 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,src,event) 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
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.

![]() | Object Precedence in Expressions Using Operators | Events — Sending and Responding to Messages | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |