Click here to Skip to main content
15,918,003 members
Articles / Programming Languages / Visual Basic

Building COM Components Using MATLAB - Part I

Rate me:
Please Sign up or sign in to vote.
3.28/5 (15 votes)
27 Feb 20046 min read 276.8K   2K   46   68
Building COM components that can be called from any IDEs that support COM with MATLAB COMBuilder toolbox

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

To create a simple COM component in MATLAB, you must pass four steps:

  • Creating a project
  • Managing m-files and MEX-files
  • Building project
  • Packaging and distributing the component

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.

Image 1

To change project settings, select Settings from Project menu. Figure 2 shows the project settings dialog:

Project Settings

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:

_install.bat Script run by the self-extracting executable
<ComponentName_ProjectVersion>.dll Compiled component
mglinstaller.exe MATLAB math and graphics installer
mwcomutil.dllCOM Builder utility library
mwregsvr.exeExecutable that registers DLLs on target machines

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

  • mglinstaller installs the MATLAB C/C++ Math and Graphics libraries.
  • mwregsvr registers mwcomutil.dll and <ComponentName_ProjectVersion>.dll.

You must repeat this distribution process on each target machine.

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:

  1. Adding Class Methods and Properties to COM Builder Objects
  2. Adding Events to COM Builder Objects
  3. Creating an Instance of a Class
  4. Calling the Methods of a Class Instance
  5. Processing varargin and varargout Arguments
  6. Handling Errors During a Method Call
  7. 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:


function [n]=AddNoise()
global x;       %Original Signal
global y;       %Noisy Signal

if (isempty(x))
n = [];

y = x + 2*randn(size(x));       %add some noise

title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')


And PowerSpectrum.m

function [m]=PowerSpectrum()
global x;       %Original Signal
global y;       %Noisy Signal

if (isempty(y))
m = [];

f = 1000*(0:256)/512;
Y = fft(y,512);
Pyy = Y.* conj(Y) / 512;
title('Frequency content of y')
xlabel('frequency (Hz)')

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):

Image 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:

References of project

OK. Click on your VB form add the following code:

Private Sub Form_Load()
Dim myVar As New Fourier.Fourier
Dim x(1 To 601) As Double
Dim t(1 To 601) As Double

m = 1
For i = 0 To 0.6 Step 0.001
    t(m) = i
    x(m) = Sin(2 * pi * 50 * t(m)) + Sin(2 * pi * 120 * t(m))
    m = m + 1

myVar.x = x
Call myVar.addnoise(0, 0)
Call myVar.powerspectrum(0, 0)
End Sub

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

Image 5

You have simply created a COM component from MATLAB!

Press F5 to see results. Figure 6 shows it.

Image 6



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.

A list of licenses authors might use can be found here

Written By
CEO Solaris Electronics LLC
United Arab Emirates United Arab Emirates
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 Acquisition 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 competition, my articles are:

You can see list of my articles, by clicking here

Comments and Discussions

Questionx64 version of a COM Pin
Moharram16-Oct-16 23:53
Moharram16-Oct-16 23:53 
Questionnice Pin
amirmanesh9-Jul-13 11:39
amirmanesh9-Jul-13 11:39 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 19:53
professionalManoj Kumar Choubey26-Feb-12 19:53 
QuestionError " Active X componenet cant creat object " in your vb project using your Fourier dll Pin
supratimgggg4-Feb-09 4:24
supratimgggg4-Feb-09 4:24 
AnswerRe: Error " Active X componenet cant creat object " in your vb project using your Fourier dll Pin
Elbe Miranda8-Dec-11 5:25
Elbe Miranda8-Dec-11 5:25 
kambiz rahbar26-Jan-09 23:59
kambiz rahbar26-Jan-09 23:59 
Questionplz give me quick reply Pin
uday divekar7-May-08 1:07
uday divekar7-May-08 1:07 
QuestionMATLAB Excel Builder ERROR : "Input argument 'x' is undefined " Pin
Pallavi00110-Mar-08 0:14
Pallavi00110-Mar-08 0:14 
Questionparallel matlab Pin
yaici malika14-Apr-07 5:41
yaici malika14-Apr-07 5:41 
Questionproblem com in VB please help me Pin
safa rafiei10-Feb-07 19:56
safa rafiei10-Feb-07 19:56 
QuestionWhere can I get Matlab.h Pin
Phani Pradeep1-Nov-06 20:10
Phani Pradeep1-Nov-06 20:10 
AnswerRe: Where can I get Matlab.h Pin
Abbas_Riazi2-Nov-06 1:54
professionalAbbas_Riazi2-Nov-06 1:54 
GeneralRe: Where can I get Matlab.h Pin
Phani Pradeep6-Nov-06 2:27
Phani Pradeep6-Nov-06 2:27 
QuestionMatlab application in vC++ i need quick help please Pin
Sangeetha_16835-Sep-06 20:41
Sangeetha_16835-Sep-06 20:41 
QuestionUnable to locate compiler. Pin
wilsonxue26-Aug-06 21:26
wilsonxue26-Aug-06 21:26 
GeneralCompiling EXCEL/COM with Matlab7/R14 and VISUAL C++ Tool Kit 2003 Pin
mindfighter218-Jul-06 14:39
mindfighter218-Jul-06 14:39 
GeneralRun time Error Pin
amira_palestine28-Jun-06 11:09
amira_palestine28-Jun-06 11:09 
AnswerRe: Run time Error Pin
Abbas_Riazi28-Jun-06 22:53
professionalAbbas_Riazi28-Jun-06 22:53 
QuestionPlease help in VC++ Pin
fdghgf19-Jun-06 22:38
fdghgf19-Jun-06 22:38 
QuestionVC++ Sample Pin
fdghgf19-Jun-06 22:33
fdghgf19-Jun-06 22:33 
AnswerRe: VC++ Sample Pin
Abbas_Riazi19-Jun-06 23:44
professionalAbbas_Riazi19-Jun-06 23:44 
GeneralI Can't Use It With Vc++ Pin
pirooz806013-Jun-06 19:23
pirooz806013-Jun-06 19:23 
AnswerRe: I Can't Use It With Vc++ Pin
Abbas_Riazi19-Jun-06 23:42
professionalAbbas_Riazi19-Jun-06 23:42 
Generalcalling com object from c# [modified] Pin
amal028-Jun-06 11:06
amal028-Jun-06 11:06 
Generalc# with COM compiler Pin
amira_palestine6-Jun-06 22:17
amira_palestine6-Jun-06 22:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.