MATLAB COM Integration for Engineering Applications

The most powerful idea around component-based software, is that components can be implemented by a programmer and reused by others without having knowledge of the source code. Components are binary packages that can be deployed and further integrated with others written on different programming languages. As component selection and integration is usually an easy and well-known process, components are also called COTS (Commercial Off-The-Shelf).


Introduction
COM (Component Object Model) is a Microsoft framework designed for Windows platforms for developing and integrating software components.Software components and reusability techniques have interesting advantages, as component base software engineering has shown through the last years.
The most powerful idea around component-based software, is that components can be implemented by a programmer and reused by others without having knowledge of the source code.Components are binary packages that can be deployed and further integrated with others written on different programming languages.As component selection and integration is usually an easy and well-known process, components are also called COTS (Commercial Off-The-Shelf).
Software components are also very useful for evaluating several implementations for different vendors.Engineers can analyse and compare them in terms of cost, performance and security.Furthermore, component software integration is a key tool for rapidprototyping software developments.
A component may be implemented with a high specialized language suitable for specific tasks and used by clients written on more general languages.For example, we could be interested on implementing a specialized component in Matlab, and integrating it into a GUI written on Visual Basic or Tcl/Tk.This situation may be comparable to software written on assembly language (specific and low-level) and linked into C programs using libraries or object code.
On the other hand, using software components also involves some risks.Software development must follow its own methodologies, standards and rules that must be taken into account when integrating external objects made from third parties.Fortunately this can be accomplished by following the rules that the component-based software methodologies suggest.
Microsoft has developed some technologies around COM. OLE (Object Linking and Embedding) and DDE (Dynamic Data Exchange) were the first tools capable of transferring objects between applications and creating links among them.The next sections will describe COM technology and related Matlab resources.Section 2 explains COM basis and history, as well as some terminology that will be useful to the reader for better understanding the following concepts.Section 3 describes Matlab COM interface, specifically the COM automation server and interface methods.Some real examples given will be useful to the novel engineer that wants to work with COM technology.In Section 4, a real application called XBDK that makes use of COM services is described.Finally, some conclusions are made to summarize this technology and to give the reader the opportunity to explore deeper inside COM and .Net technology.

Microsoft COM technology
A COM component is an instance of the component object class that runs on the COM server and is accessible from a variety of clients.There are several platforms that can serve COM objects and many clients that can use them.
Matlab COM components are very useful to integrate tasks implemented on this language and exported to others applications.These components can be used later in Microsoft Office Applications (for example Microsoft Excel), Microsoft Visual C++, C#, VB, Tcl/Tk, or even other Matlab clients, in local or distributed applications.

COM interfaces
COM component implementation is hidden to clients through convenient encapsulation, as the only way to access the component is through a public interface.An interface is a set of public methods, events and properties declarations defining the way an encapsulated object can be accessed.The component manufacturer is responsible for providing the corresponding interface information, with the necessary method details.As this information is all the knowledge of the component, it is necessary to give as much information as possible in order to ensure the component is suitable to be integrated into a third party system.Interface details are usually transparent to users.Applications as Microsoft Visual Studio provide tools for using COM objects and other resources for .Net platforms (Gunderloy, 2001).
There are four basic COM interfaces:

COM clients
A COM client is a program that uses COM Objects that are provided by COM servers.An example can be a spreadsheet built with Microsoft Excel integrated into a Matlab client.Matlab can be used as a COM client or server.
In this chapter, we are concerned with developing COM server objects using Matlab.Therefore, Matlab client details will be omitted.Depending on the locations of the component and client the server can be one of these two types:


In-Process server.In this configuration, the client accesses the components using a DLL (dynamic link library) or an ActiveX.Both client and server run in the same process so they share a unique context.


Out-Process server.The component is implemented as an independent executable (.exe) file.The client can access this program with either local or remote configuration.It depends on the location of the client and server.For local access, performance can be reduced compared to an In-Process configuration.For remote access configuration, it can be only accomplished on systems supporting DCOM (Distributed COM).

Programmatic identifiers
A programmatic identifier is a unique string that identifies an instance of a COM object.It is usually defined by the vendor.
Users can access different services using different identifiers.For example, Matlab provides three identifiers with several versions to provide such services.If there were more than one version of the software installed, each one would have its own identifier.
Figure 1 shows how a programmatic identifier can be found.It corresponds to an object available in the Windows registry.Besides finding out the existing programmatic identifiers, explore the registry (regedit32.exe) is a good way to verify if the COM servers are properly installed.Another option is to use the Microsoft Visual Studio object explorer or reference explorer.This is very useful if the client must start an instance of a specific version of the software, because each version has its own programmatic identifier.The identifier has also a suffix with a major, followed by a minor, version to identify it (for example 7.3).If no identifier is used, the most recently version will be selected.This situation is not very common, but it can be used to compare the different behaviour of the different versions.

Matlab COM server
Matlab COM server is included in the default software installation.With COM enabled, developers can implement COM components and use other COM objects.Developers may insert Matlab applications on web (html code) pages or use Microsoft Excel spreadsheets to present numerical results.
Integration of COM Matlab objects can also be distributed, so components may be run from remote machines.This is mainly used for developing parallel solutions, which are typical on many engineering applications, specifically in digital signal processing.
The next sections will explain how Matlab COM server works.Furthermore, COM objects are illustrated with some real examples.

Matlab programmatic identifiers
As shown above in figure 1, Matlab provides three programmatic identifiers to access COM servers: The single attribute will make the server to run on an exclusive mode.Otherwise the server will starts on a shared configuration.

Linking references to COM servers
When using developing tools as Microsoft Visual Studio, some previous configuration must be made before writing the code.Specifically, COM references must be included into the project configuration.Figures 2 and 3 show how these references can be added: By clicking on project tab and selecting "add reference", the references window appears, and shows the applications that provides COM servers (figure 3).The reference selected is identified by a version number, and a path for the library location.
If the server is located on a remote machine, then, the identifier will contain the path to the server having Matlab installed.The next figure shows a remote server accessible by the path "\\hiseuibd01\home$\matlab2\bin": References to other different hosts make it possible to access Matlab remote servers using DCOM.With this configuration Matlab software must be also installed on the local host, because some Matlab components must be accessible locally.
Once the references are added, Matlab objects links can be used in the source code.This is usually made by the "new" construct or "CreateObject" function.If the interface IMLApp is used, the methods available can be shown by the source code interactive help system.Otherwise methods can be viewed using the object explorer menu option (figure 5 and 6):  Figure 6 shows the available interface methods.The most useful are:  MLApp.Execute().This method executes a Matlab command on the COM server. MLApp.Feval().This method evaluates a Matlab function.


MLApp.GetFullMatrix().This method is useful to copy a matrix from Matlab workspace. MLApp.PutFullMatrix().This method is useful to copy a matrix into Matlab workspace.


MLApp.PutWorkspaceData().This method is used to put variables into Matlab workspace.


MLApp.GetWorkspaceData().This method is used to read variables from Matlab workspace.


MLApp.Quit().It must be used to force exit when the host application finalizes.

Accessing Matlab resources from the source code
The next sections will describe how to create and reference COM objects using different languages.

Creating COM objects
Microsoft .Net technologies provide resources to access COM objects from different languages.For convenience, the following examples are written in Visual Basic.Users that prefer other languages may use COM objects and references on the same way.
The following example makes use of the easy graphical user interface that provides Visual Basic.Matlab specific routines are accessed by adding references into the Visual Basic code.The next code is an example of running a Matlab server that exchanges data from the workspace.The application simply runs an interactive tool to access some Matlab resources using COM services.Note that before using COM objects, some declarations must be made in order to instantiate the objects.
Figure 7 shows how the "execute()" method can be used for running commands inside the Matlab environment.Methods "GetFullMatrix()" and "PutFullMatrix()" are used in the example to exchange data between the application and the Matlab workspace.Matlab Execute() method provides a powerful tool to use Matlab server.It is probably the most frequent method used.The source code associated with the task shown in figure 7 is the following: Figure 8 shows how the Feval() method can be used to evaluate a trigonometric function inside Matlab environment.This method allows parameter exchange, so you can pass or receive the necessary arguments (sin(π) in the example).The source code fragment for the application that it is shown in figure 8  Finally, the next code fragment shows an example for transferring data between the application and Matlab workspace, using "PutWorkspaceData()" and "GetWorkspaceData()" interface methods: Both examples transfer variables using the default "base" workspace and custom variable names read from a text box.

Creating COM objects using Tcl/Tk
Tcl (Tool Command Language) is a very powerful and easy language to learn.It is useful for automation, test, programming embedded systems, web applications and database access (Tcl community, 2009).
It was developed in 1988 by John K. Ousterhout at the University of California, and later maintained by Sun Microsystems Laboratories by the group SunScript.One of its great advantages is that it is multiplatform.There are versions for Windows, Mac OS X and most versions of Unix: Linux, Solaris, IRIX, AIX, BSD...The language is constantly evolving, so new versions of interpreters are frequently released, along with extensions that extend the functionality of the language.
Tcl is flexible and open source, so any developer can investigate the language details, include new features, modify existing ones, and even develop new commercial versions.Extensions are usually of the same type of license, although it depends on each developer.Associated with Tcl, there is a toolkit for developing graphical user interface called Tk (Tool Kit), which is the most popular Tcl extension.Tk was also developed by John Ousterhout and provides an interpreter that adds Tcl commands and others capable of creating graphical user interface components such as buttons, panels, combo boxes and dialog boxes.It is usually distributed into a package called Tcl/Tk.
Recent versions of Tcl/Tk include a package to access COM objects through a very extensive API, replacing the traditional interpreter by a compiler that translates source code to bytecodes, which then runs another interpreter (Huang, 2006).Although this improvement allows substantial increase of the execution speed, it cannot be still compared with other compiled languages.
The following code shows how Matlab COM server can be created using a Tcl/Tk script: package require tcom set application [::tcom::ref createobject "Matlab.Application.7"]Table 5. Example creating a COM reference from Tcl.
Once the reference is created, all methods will be accessible.The next lines show how to run a Matlab .m program and get the result (Matlab diagram variable) from the "base" workspace, into the local variable "diagrama".

XBDK, a TCL/TK platform using Matlab and other COM objects
XBDK stands for XML-Based Beamforming Development Kit.It is a software platform designed for aiding the engineer through the beamforming development process (Raboso et al., 2003(Raboso et al., , 2007(Raboso et al., , 2009)).It is a CASE (Computer Aided Software Environment) tool that integrates several interesting applications related to software modelling, simulation and XML parsing and manipulation.The applications are written in Java and Matlab, and are integrated using a script language (Tcl/Tk).Three COM servers are used for implementing different tasks:


Matlab COM server is used for running Matlab programs for simulation tasks.


Altova XML COM server is used for XML parsing and validation.


Microsoft Excel COM server is used for data management.
XBDK uses a data model that gives a high-level abstraction management of a variety of signal processing systems, specifically for Beamformers (Raboso et al. 2003).Beamformers are array signal processing systems that process the receiving or transmitting signals of an array of sensors (antennas) for obtaining interesting radiation properties as high directivity, low sidelobe levels or nulls for some directions of arrivals.Beamformers developed with XBDK are described using XML language, providing a natural human description of such systems.The abstraction model architecture is shown in figure 10.The specific tools to manage the information of each level are located at the right side of each layer.Beamformers are designed and later written in XML using two abstraction levels corresponding to the upper two levels of the pyramid.For XML data manipulation, files must be well-formed and validated.As XBDK is also responsible of these tasks, this tool integrates several routines that use another COM server designed by Altova (AltovaXML).
The next figure and source code below shows a .Net example performing validation and well-forming tasks.Note that the Altova COM server must be started before the methods are called.As a result, a window with the server is automatically launched.This window can be push into background until the application is closed.As the quit() method is not implemented in the interface, close operation must be done manually with CTRL+C key combination or using the task manager.The following code corresponds to the example below:

Conclusions
COM technology is a useful tool for integrating software from different vendors.This is specifically interesting for engineering applications, which have to integrate a great variety of software functionality, from specialized and low-level task, to intuitive GUIs.
Fortunately, software component industry has provided solutions to properly combine software components to get a complete solution without implementing the software from scratch.This can be made using reusability techniques following the standards defined by the Component Base Software Engineering (CBSE).
Engineers working with Matlab and other software can take advantage of CBSE using COM and .Net technologies from Microsoft.Furthermore, integrating different objects from different applications accelerates software development and reduces costs.Today, COTS (Commercial Off-The-Shelf) components are ready to be integrated into the engineering applications and vendors exploit these advantages developing components while assuring quality and standard conforming.
After reading this chapter, I expect that the interested reader can take into account component technology on their future projects, and gain effectiveness on the overall software development process.

Figure 1 .
Figure 1.Matlab programmatic identifiers for different package versions.

Figure 2 .
Figure 2. Visual Studio tool for adding external references.

Figure 4 .
Figure 4. Matlab COM references for different package versions and locations.
They were available on the earliest Windows versions.In 1996 OLE technology was fused with Internet capabilities and was renamed as ActiveX, providing ActiveX controls, Active Documents and Active Scripting.COM+ are COM based services first developed for Windows 2000.They extend COM technology with advanced services to manage resource pools, disconnected applications and event publication.DCOM (Distributed Component Object Model) is a Microsoft technology that enables communication links among distributed components running on network interconnected machines.DCOM extends COM and COM+ using new services based on DCE (Distributed Computing Environment) and RPC (Remote Procedure Call).Nowadays, Microsoft recommends using .Net technology instead, as it has already integrated COM services.Many powerful programming languages can use COM components; Visual Basic, Matlab, Visual C++, C# and Tcl/Tk are good examples.
Matlab client (Out-Process Server).Client accesses server resources using an executable (.exe) file.The component instantiates on a separate process.Communication is not as effective as in the In-Process configuration.Matlabapplication and Matlab server (Automation Server).Using this configuration, a client application (called controller) accesses the services provided by the Automation server.The services make possible to run commands and transfer variables into Matlab workspace.The server may run locally or on a remote machine using DCOM services (see section 2.3).This configuration is affected from networks issues, specifically by bandwidth bottlenecks and latency problems.Clientapplication and Matlab server (engine server).Matlab offers a faster interface called IEngine to be used with C, C++ and Fortran clients.
Communication is extremely efficient as both share the same context.If a DLL is used, it runs on a separate window.

Table 7 .
Example using AltovaXML COM server.