Create Microsoft ActiveX control in figure window


h = actxcontrol('progid')
h = actxcontrol('progid','param1',value1,...)
h = actxcontrol('progid',position)
h = actxcontrol('progid', position, fig_handle)
h = actxcontrol('progid',position,fig_handle,event_handler)
h = actxcontrol('progid',position,fig_handle,event_handler,'filename')


h = actxcontrol('progid') creates an ActiveX® control in a figure window. The programmatic identifier (progid) for the control determines the type of control created. (See the documentation provided by the control vendor to get this string.) The returned object, h, represents the default interface for the control.

You cannot use an ActiveX server for the progid because MATLAB® software cannot insert ActiveX servers in a figure. See actxserver for use with ActiveX servers.

h = actxcontrol('progid','param1',value1,...) creates an ActiveX control using the optional parameter name/value pairs. Parameter names include:

  • position — MATLAB position vector specifying the position of the control. The format is [left, bottom, width, height] using pixel units.

  • parent — Handle to parent figure, model, or Command Window.

  • callback — Name of event handler. To use the same handler for all events, specify a single name. To handle specific events, specify a cell array of event name/event handler pairs.

  • filename — Sets the initial conditions to the previously saved control.

  • licensekey — License key to create licensed ActiveX controls that require design-time licenses. See Deploy ActiveX Controls Requiring Run-Time Licenses for information on how to use controls that require run-time licenses.

One possible format is:

h = actxcontrol('myProgid','newPosition',[0 0 200 200],...
	'myCallback',{'Click' 'myClickHandler';...
	'DblClick' 'myDblClickHandler';...
	'MouseDown' 'myMouseDownHandler'});

The following syntaxes are deprecated and will not become obsolete. They are included for reference, but the previous syntaxes are preferred.

h = actxcontrol('progid',position) creates an ActiveX control having the location and size specified in the vector, position. The format of this vector is:

[x y width height]

The first two elements of the vector determine where the control is placed in the figure window, with x and y being offsets, in pixels, from the bottom left corner of the figure window to the same corner of the control. The last two elements, width and height, determine the size of the control itself.

The default position vector is [20 20 60 60].

h = actxcontrol('progid', position, fig_handle) creates an ActiveX control at the specified position in an existing figure window. The Handle Graphics® handle, fig_handle, identifies this window.

The gcf command returns the current figure handle.

    Note   If the figure window designated by fig_handle is invisible, the control is invisible. If you want the control you are creating to be invisible, use the handle of an invisible figure window.

h = actxcontrol('progid',position,fig_handle,event_handler) creates an ActiveX control that responds to events. Controls respond to events by invoking a MATLAB function whenever an event (such as clicking a mouse button) is fired. The event_handler argument identifies one or more functions to be used in handling events. For more information, see Specifying Event Handlers.

h = actxcontrol('progid',position,fig_handle,event_handler,'filename') creates an ActiveX control with the first four arguments, and sets its initial state to that of a previously saved control. MATLAB loads the initial state from the file specified in the string filename.

If you do not want to specify an event_handler, you can use an empty string ('') as the fourth argument.

The progid argument must match the progid of the saved control.

Specifying Event Handlers

There is more than one valid format for the event_handler argument. Use this argument to specify one of the following:

  • A different event handler routine for each event supported by the control

  • One common routine to handle selected events

  • One common routine to handle all events

In the first case, use a cell array for the event_handler argument, with each row of the array specifying an event and handler pair:

{'event' 'eventhandler'; 'event2' 'eventhandler2'; ...}

event is either a string containing the event name or a numeric event identifier (see Example 2). eventhandler is a string identifying the function you want the control to use in handling the event. Include only those events that you want enabled.

In the second case, use the same cell array syntax described, but specify the same eventhandler for each event. Again, include only those events that you want enabled.

In the third case, make event_handler a string (instead of a cell array) that contains the name of the one function that is to handle all events for the control.

There is no limit to the number of event and handler pairs you can specify in the event_handler cell array. However, if you register the same event name to the same callback handler multiple times, MATLAB executes the event only once.

Event handler functions accept a variable number of arguments.

Strings used in the event_handler argument are not case-sensitive.

    Note   Although using a single handler for all events might be easier in some cases, specifying an individual handler for each event creates more efficient code, resulting in better performance.


Handling Events

The event_handler argument specifies how you want the control to handle any events that occur. The control can handle all events with one common handler function, selected events with a common handler function, or each type of event can be handled by a separate function.

This command creates an mwsamp control that uses one event handler, sampev, to respond to all events:

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

The next command also uses a common event handler, but will only invoke the handler when selected events, Click and DblClick are fired:

h = actxcontrol('mwsamp.mwsampctrl.2',[0 0 200 200],...
   gcf,{'Click' 'sampev'; 'DblClick' 'sampev'});

This command assigns a different handler routine to each event. For example, Click is an event, and myclick is the routine that executes whenever a Click event is fired:

h = actxcontrol('mwsamp.mwsampctrl.2',[0 0 200 200],...
   gcf,{'Click', 'myclick'; 'DblClick' 'my2click'; ...
   'MouseDown' 'mymoused'});

The next command does the same thing, but specifies the events using numeric event identifiers:

h = actxcontrol('mwsamp.mwsampctrl.2',[0 0 200 200],...
gcf,{-600, 'myclick'; -601 'my2click'; -605 'mymoused'});

For examples of event handler functions and how to register them with MATLAB, see Sample Event Handlers.

More About

expand all


If the control implements any custom interfaces, use the interfaces function to list them, and the invoke function to get a handle to a selected interface.

When you no longer need the control, call release to release the interface and free memory and other resources used by the interface. Releasing the interface does not delete the control itself. To release the interface, use the delete function.

For more information on handling control events, see Writing Event Handlers.

For an example event handler, see the file sampev.m in the toolbox\matlab\winfun\comcli folder.

COM functions are available on Microsoft® Windows® systems only.

    Note   If you encounter problems creating Microsoft Forms 2.0 controls in MATLAB or other non-VBA container applications, see Use Microsoft Forms 2.0 Controls.

Was this topic helpful?