COM Event Handlers

Overview of Event Handling

For controls, register handler functions either at the time you create an instance of the control (using actxcontrol), or any time afterward (using registerevent).

For servers, use registerevent to register events.

Use events to list all the events a COM object recognizes.

Arguments Passed to Event Handlers

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

Arguments Passed by MATLAB Functions

Arg. No.ContentsFormat

1

Object name

MATLAB COM class

2

Event ID

double

3

Start of Event Argument List

As passed by the control

end-2

End of Event Argument List (Argument 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:

function event (varargin)
if (strcmp(varargin{end}, 'MouseDown'))  % Check the event name
    x_pos = varargin{5};              % Read 5th Event Argument
    y_pos = varargin{6};              % Read 6th Event Argument
end

Note

The values passed vary with the particular event and control being used.

Event Structure

The Event Structure argument passed by MATLAB contains the fields shown in the following table.

Fields of the Event Structure

Field NameDescriptionFormat

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:

    Type: 'MouseDown'
  Source: [1x1 COM.mwsamp.mwsampctrl.2]
 EventID: -605
  Button: 1
   Shift: 0
       x: 27
       y: 24

See Also

| |

Related Examples

More About