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 applations.
Building Steps
To create a Simple Comss 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 ..........................
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 shouth...
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.To change project settings, select Settings from Project menu Figure 2 shows the project settings dialog...:
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 "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.:
FilePurpose_install.bat script run by the self-extracting executable.dll
Compiled Componentmglinstaller.exe Matlab Math and Graphics Installermwcomutil.dllcom Builder Utility Librarymwregsvr.exeexecutable That Registers Dlls on Target Machines
The self-extracting executable is name
.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
.
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:
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
Add 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 .
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). There Two M-Files Are As Below:
AddNoise.m
Function [N] = addNoise () Global X;
% Original Signal Global Y;
% Noisy Signal
IF (ISEMPTY (X)) n = [];
Return;
end
Y = x 2 * randn (size (x));
% Add Some Noise
Subplot (211)
Plot (Y (1:50))
Title ('Signal Corrupted with Zero-Mean Random Noise')
XLabel ('Time (Milliseconds)')
N = Y;
And PowerSpectrum.m
Function [M] = PowerSpectrum ()
Global X;
% Original Signal Global Y;
% Noisy Signal
IF (ISEMPTY (Y))
m = [];
Return;
end
f = 1000 * (0: 256) / 512;
Y = fft (y, 512);
PYY = Y. * Conj (Y) / 512;
Subplot (212)
Plot (f, pyy (1: 257)))
Title ('Frequency Content of Y')
XLabel ('Frequency (HZ)')
m = pyy;
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 the 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 ComtoL. Figure 4 Shows The References of The Project: OK. Click ON Your VB Form Add The Following Code:
Private sub flow_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
NEXT
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 method 4:
You Have SIMPLY CREATED A COM Component from Matlab!
Press F5 to See Results. Figure 6 shows it.
Enjoy!
About A. Riazi
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 and August (2003) best article of month competetion, my articles are: Add GPS Support to your Desktop Solving Engineering Problem Using MATLAB C API Text2PDF You can see list of my articles, by clicking here