Display Event Messages from mwsamp Control

This example shows how to handle events fired by the MATLAB® ActiveX® control, mwsamp2.

Create Event Handler Routines

Create event handler files and save them to a folder on your path.

Create the myclick.m file.

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

Create the my2click.m file.

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

Create the mymoused.m file.

function mymoused(varargin)
disp('You have triggered the mouse down function')
disp('The X position is: ')
disp('The Y position is: ')

Create Control and Register Events

Open a figure window.

f = figure('position', [100 200 200 200]);

Register the Click and MouseDown events.

ctrl = actxcontrol('mwsamp.mwsampctrl.2',[0 0 200 200],f,...
    {'Click','myclick'; 'MouseDown','mymoused'});

Respond to Control Events

When MATLAB creates the mwsamp2 control, it also displays a figure window showing a label and circle at the center. If you click different positions in this window, you see a report in the MATLAB Command Window of the X and Y position of the mouse.

Each time you press the mouse button, the MouseDown event fires, calling the mymoused function. This function prints the position values for that event to the Command Window. For example:

The X position is: 
ans = 
The Y position is: 
ans = 

The Click event displays the myclick message.

Single click function

Double-clicking the mouse does nothing, since the DblClick event is not registered.

Register DblClick Event


When you double-click the mouse, MATLAB displays both the myclick and my2click messages.

Single click function
Double click function

Display Control Events

Unregister the DblClick event.


Display the currently registered events.

ans = 
    'click'        'myclick' 
    'mousedown'    'mymoused'

Unregister All Events

ans = 

Clicking the mouse in the control window now does nothing since there are no active events.

Close the figure window.

See Also

| | |

Related Examples

More About