MATLAB® software provides two functions to create a COM object:
actxcontrol
—
Creates a Microsoft® ActiveX® control in a MATLAB figure window.
actxserver
—
Creates an in-process server for a dynamic link library (DLL) component
or an out-of-process server for an executable (EXE) component.
The following diagram shows the basic steps in creating the server process. For more information on how the MATLAB software establishes interfaces to the resultant COM object, see COM Object Interfaces.
You can create an instance of an ActiveX control from the MATLAB client
using either a user interface (actxcontrolselect
)
or the actxcontrol
function
from the command line. Either of these methods creates an instance
of the control in the MATLAB client process and returns a handle
to the primary interface to the COM object. Through this interface,
you can access the public properties or methods of the object. You
can also establish more interfaces to the object, including interfaces
that use IDispatch, and any custom interfaces that might exist.
This section describes how to create an instance of the control and how to position it in the MATLAB figure window.
The actxcontrollist
function
shows you what COM controls are currently installed on your system.
Type:
list = actxcontrollist
MATLAB displays a cell array listing each control, including its name, programmatic identifier (ProgID), and file name.
This example shows information for several controls (your results might be different):
list = actxcontrollist; s = sprintf(' Name = %s\n ProgID = %s\n File = %s\n', list{114:115,:})
MATLAB displays:
s = Name = OleInstall Class ProgID = Outlook Express Mime Editor File = OlePrn.OleInstall.1 Name = OutlookExpress.MimeEdit.1 ProgID = C:\WINNT\System32\oleprn.dll File = C:\WINNT\System32\inetcomm.dll
If you know the name of a control, you can display its ProgID
and the path of the folder containing it. For example, some of the
examples in this documentation use the Mwsamp2
control.
You can find it with the following code:
list = actxcontrollist; for ii = 1:length(list) if ~isempty(strfind([list{ii,:}],'Mwsamp2')) s = sprintf(' Name = %s\n ProgID = %s\n File = %s\n', ... list{ii,:}) end end
s = Name = Mwsamp2 Control ProgID = MWSAMP.MwsampCtrl.2 File = D:\Apps\MATLAB\R2006a\toolbox\matlab\winfun\win32\mwsamp2.ocx
The location of this file depends on your MATLAB installation.
Using the actxcontrolselect
function
is the simplest way to create an instance of a control object. This
function displays all controls installed on your system. When you
select an item from the list and click the Create button, MATLAB creates
the control and returns a handle to it. Type:
h = actxcontrolselect
MATLAB displays the Select an ActiveX Control dialog box.
The interface has an ActiveX Control List selection pane on the left and a Preview pane on the right. To see a preview of the control, click one of the control names in the selection pane. (A blank preview pane means that the control does not have a preview.) If MATLAB cannot create the instance, an error message appears in the preview pane.
Setting Properties with actxcontrolselect
. To change property values when creating the control, click the Properties button
in the Preview pane. You can select which figure
window to put the control in (Parent field),
where to position it in the window (X and Y fields), and what size to make the control
(Width and Height).
You can register events you want the control to respond to in this window. Enter the name of the routine to the right of the event under Event Handler.
You can select callback routines by clicking a name in the Event column, and then clicking the Browse button. To assign a callback routine to more than one event, first press the Ctrl key and click individual event names. Alternatively, drag the mouse over consecutive event names, and then click Browse to select the callback routine.
MATLAB only responds to registered events, so if you do not specify a callback, the event is ignored.
For example, in the ActiveX Control List pane,
select Calendar Control 10.0 (the
version on your system might be different) and click Properties. MATLAB displays
the Choose ActiveX Control Creation Parameter dialog box. To change
the default size for the control, enter a Width of 500
and
a Height of 350
.
To create an instance of the Calendar control, click OK in
this window, and click Create in
the next window.
You can also set control parameters using the actxcontrol
function.
One parameter you can set with actxcontrol
, but
not with actxcontrolselect
, is the name of an
initialization file. When you specify this file name, MATLAB sets
the initial state of the control to that of a previously saved control.
Information Returned by actxcontrolselect
. The actxcontrolselect
function creates
an object that is an instance of the MATLAB COM class. The function
returns up to two arguments: a handle for the object, h
,
and a 1
-by-3
cell array, info
,
containing information about the control. To get this information,
type:
[h, info] = actxcontrolselect
The cell array info
shows the name, ProgID,
and file name for the control.
If you select the Calendar Control, and then click Create, MATLAB displays information like:
h = COM.mscal.calendar.7 info = [1x20 char] 'MSCAL.Calendar.7' [1x41 char]
To expand the info
cell array, type:
info{:}
MATLAB displays:
ans = Calendar Control 9.0 ans = MSCAL.Calendar.7 ans = D:\Applications\MSOffice\Office\MSCAL.OCX
If you already know which control you want and you know its
ProgID, use the actxcontrol
function to create
an instance of the control.
The ProgID is the only required input to this function. Additionally,
you can select which figure window to put the control in, where to
position it in the window, and what size to make it. You can also
register events you want the control to respond to, or set the initial
state of the control by reading that state from a file. See the actxcontrol
reference
page for a full explanation of its input arguments.
The actxcontrol
function returns a handle
to the primary interface to the object. Use this handle to reference
the object in other COM function calls. You can also use the handle
to obtain more interfaces to the object. For more information on using
interfaces, see COM Object Interfaces.
After creating a control, you can change its shape and position
in the window with the move
function.
Observe what happens to the object created in the last section
when you specify new origin coordinates (70
, 120
)
and new width and height dimensions of 400
and 350
:
move(h,[70 120 400 350]);
A MATLAB COM ActiveX control container does not in-place activate controls until they are visible.
To create a server for a component implemented as a dynamic
link library (DLL), use the actxserver
function. MATLAB creates
an instance of the component in the same process that contains the
client application.
The syntax for actxserver
, when used with
a DLL component, is actxserver(ProgID)
, where ProgID
is
the programmatic identifier for the component.
actxserver
returns a handle to the primary
interface to the object. Use this handle to reference the object in
other COM function calls. You can also use the handle to obtain more
interfaces to the object. For more information on using interfaces,
see COM Object Interfaces.
Unlike Microsoft ActiveX controls, any user interface displayed by the server appears in a separate window.
You cannot use a 32-bit in-process DLL COM object in a 64-bit MATLAB application. For information about this restriction, see http://www.mathworks.com/matlabcentral/answers/95116-why-am-i-not-able-to-use-32-bit-dll-com-objects-in-64-bit-matlab-7-3-r2006b.
You can use the actxserver
function to
create a server for a component implemented as an executable (EXE).
In this case, MATLAB instantiates the component in an out-of-process
server.
The syntax for actxserver
to create an
executable is actxserver(ProgID, sysname)
. ProgID
is
the programmatic identifier for the component, and sysname
is
an optional argument used in configuring a distributed COM (DCOM)
system.
actxserver
returns a handle to the primary
interface to the COM object. Use this handle to reference the object
in other COM function calls. You can also use the handle to obtain
more interfaces to the object. For more information on using interfaces,
see COM Object Interfaces.
Any user interface displayed by the server appears in a separate window.
This example creates a COM server application running the Microsoft Excel® spreadsheet
program. The handle is assigned to h
.
h = actxserver('Excel.Application')
MATLAB displays:
h = COM.excel.application
MATLAB can programmatically connect to an
instance of a COM Automation server application that is already running
on your computer. To get a reference to such an application, use the actxGetRunningServer
function.
This example gets a reference to the Excel program, which
must already be running on your system. The returned handle is assigned
to h
.
h = actxGetRunningServer('Excel.Application')
MATLAB displays:
h = COM.excel.application