|On this page…|
Note: MATLAB® supports the Microsoft® .NET Framework on the Windows® platform. For more information about this alternative, see Overview Using .NET from MATLAB.
The Microsoft Component Object Model (COM) provides a framework for integrating reusable, binary software components into an application. Because components are implemented with compiled code, the source code can be written in any of the many programming languages that support COM. Upgrades to applications are simplified, as components can simply be swapped without the need to recompile the entire application. In addition, a component's location is transparent to the application, so components can be relocated to a separate process or even a remote system without having to modify the application.
Using COM, developers and end users can select application-specific components produced by different vendors and integrate them into a complete application solution. For example, a single application might require database access, mathematical analysis, and presentation-quality business graphs. Using COM, a developer can choose a database-access component by one vendor, a business graph component by another, and integrate these into a mathematical analysis package produced by yet a third.
MATLAB software supports COM integration on the Microsoft Windows platform only.
While the ideas behind COM technology are straightforward, the terminology is not. The meaning of COM terms has changed over time and few concise definitions exist. Here are some terms that you should be familiar with. These are not comprehensive definitions. For a complete description of COM, you'll need to consult outside resources.
A COM object is a software component that conforms to the Component Object Model. COM enforces encapsulation of the object, preventing direct access of its data and implementation. COM objects expose Interfaces, which consist of properties, methods and events.
A COM client is a program that makes use of COM objects. COM objects that expose functionality for use are called COM servers. COM servers can be in-process or out-of-process. An example of an out-of-process server is Microsoft Excel® spreadsheet program. These configurations are described in In-Process and Out-of-Process Servers.
A Microsoft ActiveX® control is a type of in-process COM server that requires a control container. ActiveX controls typically have a user interface. An example is the Microsoft Calendar control. A control container is an application capable of hosting ActiveX controls. A MATLAB figure window or a Simulink® model are examples of control containers.
MATLAB can be used as either a COM client or COM server.
The functionality of a component is defined by one or more interfaces. To use a COM component, you must learn about its interfaces, and the methods, properties, and events implemented by the component. The component vendor provides this information.
There are two standard COM interfaces:
IUnknown — An interface required by all COM components. All other COM interfaces are derived from IUnknown.
IDispatch — An interface that exposes objects, methods and properties to applications that support Automation.
Using MATLAB as a COM client provides two techniques for developing programs in MATLAB:
You can include COM components in your MATLAB application (for example, a spreadsheet).
You can access existing applications that expose objects via Automation.
In a typical scenario, MATLAB creates ActiveX controls in figure windows, which are manipulated by MATLAB through the controls' properties, methods, and events. This is useful because there exists a wide variety of graphical user interface components implemented as ActiveX controls. For example, the Microsoft Internet Explorer® program exposes objects that you can include in a figure to display an HTML file. There also are treeviews, spreadsheets, and calendars available from a variety of sources.
MATLAB COM clients can access applications that support Automation, such as the Excel spreadsheet program. In this case, MATLAB creates an Automation server in which to run the application and returns a handle to the primary interface for the object created.
Information about creating and using COM controls and server objects in MATLAB can be found in Creating COM Objects.
Automation provides an infrastructure whereby applications called automation controllers can access and manipulate (i.e. set properties of or call methods on) shared automation objects that are exported by other applications, called Automation servers. Any Windows program that can be configured as an Automation controller can control MATLAB.
For example, using Microsoft Visual Basic® programming language, you can run a MATLAB script in a Microsoft PowerPoint® presentation. In this case, PowerPoint is the controller and MATLAB is the server.
Information for creating and connecting to a MATLAB Automation server running MATLAB can be found in MATLAB COM Automation Server Interface.
Before using COM objects, you must register their controls and servers. Most are registered by default. However, if you get a new .ocx, .dll, or other object file for the control or server, you must register the file manually in the Windows registry.
Use the Windows regsvr32 command to register your file. From the Windows prompt, use the cd function to go to the folder containing the object file. If your object file is an .ocx file, type:
For example, to register the MATLAB control mwsamp2.ocx, type:
cd matlabroot\toolbox\matlab\winfun\win32 regsvr32 mwsamp2.ocx
If you encounter problems with this procedure, please consult a Windows manual or contact your local system administrator.
If you create a COM control using Microsoft .NET Framework 4, use the DOS regasm command with the /codebase option to register your file.
Here are several ways to verify that a control or server is registered. These examples use the MATLAB mwsamp control. Refer to your Microsoft product documentation for information about using Microsoft Visual Studio® or the Microsoft Registry Editor programs.
Go to the Visual Studio .NET 2003 Tools menu and execute the ActiveX control test container. Click Edit, insert a new control, and select MwSamp Control. If you are able to insert the control without any problems, the control is successfully registered. Note that this method only works on controls.
Open the Registry Editor by typing regedit at the DOS prompt. Search for your control or server object by selecting Find from the Edit menu. It will likely be in the following structure:
Open OLEViewer from the Visual Studio .NET 2003 Tools menu. Look in the following structure for your Control object:
Object Classes : Grouped by Component Category : Control : Your_Control_Object_Name (i.e. Object Classes : Grouped by Component Category : Control : Mwsamp Control)