What is MATLAB COM builder?

The MATLAB COM Builder is an extension to the MATLAB Compiler that enables customers to automatically convert MATLAB applications to Component Object Model (COM) objects. Developers can do modeling and analysis in MATLAB and convert the models to ready-to-use COM objects. These objects can be immediately integrated with any COM-based application.

Building steps

A project consists of all the elements necessary to build an application using the MATLAB COM Builder. COM Builder components are COM objects accessible through Visual Basic, Visual C++, Delphi, Power Builder or any other language that supports COM. Each COM object exposes one or more classes. Each class contains a set of functions called methods, corresponding to the original MATLAB functions included in the component’s project.

When creating a component, you must additionally provide one or more class names. The component name represents the name of the DLL file to be created. A class name denotes the name of the class that performs a call on a specific method at run-time. The relationship between component name and class name, and which methods (MATLAB functions) go into a particular class, are purely organizational. As a general rule, when compiling many MATLAB functions, it helps to determine a scheme of function categories and to create a separate class for each category. The name of each class should be descriptive of what the class does.

Creating a project

When creating a new project, enter the MATLAB comtool command. In this case the MATLAB COM Builder GUI will appear. The following figure shows the GUI.

The component name is name of the DLL file that is built by the build process. The component name must differ from any m-file or mex-file added later to the project. One can add more classes to the project when entering the class name and pressing "Add >>" button.

Note: If your component requires any graphics routine (such as the plot function), you must select Use Handle Graphics Library from Compiler Options.

Managing m-files and MEX-files

After creating the project, it is time to add m-files and mex-files related to the project. You can do this through "Add File" button. Editing or removing a file is very simple. Just select the file and press the "Edit" or "Remove" button! Every object that is added to the project is displayed in project tree.

Building Project

By collecting all of the necessary information, we can build the project. For this reason, select "COM Object" from the Build menu or simply click on the "Build" button in the right pane. The build status pane will show the status of build process. If any error occurs during the build process, the status pane will show it.

Packaging and Distributing the Component

Once you have successfully compiled your models and tested the COM object, you are ready to package the component for distribution to your end users. Choose Component, Package Component to create a self-extracting executable containing these files:

File

Purpose

_install.bat

Script run by the self-extracting executable

<ComponentName_ProjectVersion>.dll

Compiled component

mglinstaller.exe

MATLAB math and graphics installer

mwcomutil.dll

COM Builder utility library

mwregsvr.exe

Executable that registers DLLs on target machines

The self-extracting executable is named <componentname>.exe. Running the installer on a target machine performs these steps:

Building MATLAB Component

By building a component using the MATLAB COM Builder, a developer can use it from any language or environment that benefit from COM features such as Visual Basic, Visual C++, Access, Excel and so on. The process of integrating components built by MATLAB is easy, but in general you must address seven items in any code written to use the MATLAB COM Builder components:

Adding Class Methods and Properties to COM Builder Objects

Adding Events to COM Builder Objects

Creating an Instance of a Class

Calling the Methods of a Class Instance

Processing varargin and varargout Arguments

Handling Errors During a Method Call

Modifying Flags

Adding Class Methods and Properties to COM Builder Objects

Class properties allow an object to retain an internal state between method calls. MATLAB COM Builder automatically converts all global variables shared by the M-files that make up a class to properties on that class. By using a global keyword when declaring a variable, the variable is seen as global in the workspace. MATLAB m-files act as the class methods. Each m-file in this situation, define a class method.

Here is an example: A common use of Fourier transforms is to find the frequency components of a signal buried in a noisy time domain signal. Consider data sampled at 1000 Hz. Form a signal containing 50 Hz and 120 Hz and corrupt it with some zero-mean random noise. It is difficult to identify the frequency components by looking at the original signal. Converting to the frequency domain, the discrete Fourier transform of the noisy signal (y) is found by taking the 512-point fast Fourier transform (FFT).

We can develop two m-files, one for adding some noise to original signal and the other for computing power spectrum (a measurement of power at various frequencies). These two m-files are as below:

The first one adds some noise and the second, computes power spectrum of the noisy signal, both of the m-files plot their results. As you see we declare two global variables: x, y. Both of them are considered as properties of Fourier class. We can see them in Visual Basic later. Add both of them to MATLAB COM Builder project (Figure 3):

Now, build the component and open an IDE such as Visual Basic. To see what occured, open a new project and select "References" from Project menu. The following dialog will be displayed, select "Fourier 1.0 Type Library" from list. As you see, the location of the component is where we built the component with comtool. Figure 4 shows the References of the project:

The magic is here. When you are typing, VB shows methods and properties of your component (See figure 5):

You have simply created a COM component from MATLAB!

Press F5 to see results. Figure 6 shows it.

Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Share

About the Author

I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acqusition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competetion, my articles are:

I downloaded your codes and used your com dll (Fourier_1_0.dll) . I opened your vb project and add the refernce to Fourier library. But after that when I try to run the vb project it gives me error ...
"Active X componenet cant creat object "..and if I debug it gives a yellow color tick to "myvar.x=x" ..If I skip this line it gives error on next line....
I also tried to register Fourier dll bu regsvr32 but it syas it cant find the file and it fails ( although files was there at the same proper folder) ..I am not sure that I need to register com dll.

ANy idea how to resolve the problem will be greatly appreciated. My VB6 is working good as I checked with other programs..

i am interfacing fuzzy logic toolbox with VB.
i have created an m-file
function [o]=rap(x,y)
fismat = readfis('C:\Matlab7\work\dmg.fis');
o = evalfis([x y],fismat)
i have created the dll file and i have got the library in VB-project-references.
now i want to call the following matlab command in VB code
p = readfis('dmg.fis');
evalfis([x y],p)
whether it can be done?,
if yes what will be the VB code?
plz give me quick reply

Private Sub GetTransOutputSpeed_Click()
Dim a As Range
Dim b As Range
Dim c As Range
Set a = Worksheets("Sheet1").Range("A3:A3")
Set b = Worksheets("Sheet1").Range("B3:B3")
Set c = Worksheets("Sheet1").Range("D3:D3")
Call ChryslerTest(c, a, b)
End Sub
For now I am passing only A3:A3, B3:B3, D3:D3. But i need to pass entire coulmn i.e. A3:A34398.

I am getting error "Input argument 'x' is undefined".

After searching for the cause for the error, I acme to know that, this error gets when one doesn't pass arguments to the function. Or passed argument might be in different workspace than that of the workspace used by function call(as every function call uses deifferent workspace).

But I am not able to sort it out in this senario. Can I get the help to sort out the same?

I am an algerian student and i have to present and to develop a parallel or distributed application on matlab. Do you have any easy example or an easy way to do that (without Com builder that i dont have!!). I use matlab version 7. Thank you.

Iam Phani Pradeep working for CMCLtd. I saw your sample program to use MATLAB as computational engine from VC++. But I'm getting some errors becuase matlab.h is not available. Can you tell me where can i get the matlab.h file . I'm using MATLAB 7.0.

I invoked the m file in VC++ using Matlab Project Wizard in VC++..i encountered this error after execution.There is no error in building ,linking,compiling..
i properly set mex -setup and mbuild- setup.is there any path setup problem..i dont know to find out this problem..
ERROR
-------
The procedure entry point mxcountcolonop could not be located in the dyanmic link library libmx.dll..

any simple suggestion is very valuable for me
Thanks&regards,
Sangeetha

Greetings
Im having trouble with this. I have matlab 7/R14 and downloaded a free C+ compiler with VCToolkit 2003. First, the compiler wasnt recognized. Then i followed the steps of the mathworks technicalnotes to change the directory (Mathworks says it is possible to compile in R14 with ToolKit 2003, altough later versions of matlab dont support this). Now what i got an error :

IDL compile of 'mwcomtypes.idl' failed

How to solve that ? Mathworks says its a bug, but only only gives solution to later versions than 7/R14.

Dear sir,
i used disphelper but as soon as i started using it it is ease to use it but when you need to retrive data from vc++ it fails and there is no help available regarding that...
please help me in retriving data from COM using dhCallMethod..... confused:

Mr Riazi!
Please Help me How To Use Matlab Com With Vc++
Because I Can't Work With VARIANT As Input And Output Parameters
In VC++;
please Write A short Sample of Use With VC++;
Tanks For your Excellent Article;
A.Shamshirian;
Bojnord;

Assalam Alaike i have aproject in university and i have aproblem
i build com object sucessfully in matlab but now i need to see the output which is figure to see it from c#
i dont know how call it
and i need to acess the data from c# to matlab and show how the output change
please help me in quick time
because the project must be finish during one week
pleeese