External Interfaces Previous page   Next Page

Writing Event Handlers

This section covers the following topics on writing handler routines to respond to events fired from an ActiveX control or Automation server:

Overview of Event Handling

An event is fired when a control or server wants to notify its client that something of interest has occurred. For example, many controls trigger an event when the user clicks somewhere in the interface window of a control. In MATLAB, you can create and register your own M-file functions so that they respond to events when they occur. These functions serve as event handlers. You can create one handler function to handle all events or a separate handler for each type of event.

For controls, you can register your handler functions either at the time you create the control (using actxcontrol), or at any time afterwards (using registerevent). For servers, you must use the registerevent function to register those events you want the client to listen to. Specify the event handler in the argument list, as shown below for actxcontrol. The event handler argument can be either the name of a single callback routine or a cell array that associates specific events with their respective event handlers:

When you specify the single callback routine, MATLAB registers all events with that one routine. When any event is fired, MATLAB executes the common callback routine.

You can list all the events that a COM object recognizes using the events function. For example, to list all events for the mwsamp2 control, use

Arguments Passed to Event Handlers

When a registered event is triggered, MATLAB passes information from the event to its handler function as shown in this table.

Arguments Passed by MATLAB

Arg. No.
Contents
Format
1
Object Name
MATLAB COM class
2
Event ID
double
3
Start of Event Arg. List
As passed by the control
end-2
End of Event Arg. List (Arg. N)
As passed by the control
end-1
Event Structure
structure
end
Event Name
char array

When writing an event handler function, use the Event Name argument to identify the source of the event. Get the arguments passed by the control from the Event Argument List (arguments 3 through end-2). All event handlers must accept a variable number of arguments:

Event Structure

The second to last argument passed by MATLAB is the Event Structure, which has the following fields.

Fields of the Event Structure

Field Name
Description
Format
Type
Event Name
char array
Source
Control Name

MATLAB COM class

EventID
Event Identifier
double
Event Arg Name 1
Event Arg Value 1
As passed by the control
Event Arg Name 2
Event Arg Value 2
As passed by the control
etc.
Event Arg N
As passed by the control

For example, when the MouseDown event of the mwsamp2 control is triggered, MATLAB passes this Event Structure to the registered event handler:

Sample Event Handlers

Specify a single callback, sampev:

Or specify several events using the cell array format:

The event handlers, myclick.m, my2click.m, and mymoused.m, are

Alternatively, you can use the same event handler for all the events you want to monitor using the cell array pairs. Response time will be better than using the callback style.

For example

where allevents.m is

Writing Event Handlers Using M-File Subfunctions

Instead of having to maintain a separate M-file for every event handler routine you write, you can consolidate some or all of these routines into a single M-file using M-file subfunctions.

This example shows three event handler routines, (myclick, my2click, and mymoused) implemented as subfunctions in the file mycallbacks.m. The call to str2func converts the input string to a function handle:

To register one of these events, you just call mycallbacks, passing the name of the event handler:


Previous page  Control and Server Events Saving Your Work Next page

© 1994-2005 The MathWorks, Inc.