Combine Event Handlers as MATLAB Local Functions

This example shows how to consolidate event handlers into a single file using local functions.

Create the mycallbacks.m file containing three event handler routines, myclick, my2click, and mymoused, implemented as local functions.

function a = mycallbacks(str) 
a = str2func(str);

function myclick(varargin)
disp('Single click function')

function my2click(varargin)
disp('Double click function')

function mymoused(varargin)
disp('You have reached the mouse down function')
disp('The X position is: ')
double(varargin{5})
disp('The Y position is: ')
double(varargin{6})

The call to str2func converts the input character vector to a function handle.

Create the control.

h = actxcontrol('mwsamp.mwsampctrl.2',[0 0 200 200],gcf,'sampev')

Register Click event.

registerevent(h,{'Click',mycallbacks('myclick')})

Related Topics