Click here to Skip to main content
15,891,925 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 275.2K   2K   46  
Building COM components that can be called from any IDEs that support COM with MATLAB COMBuilder toolbox
//
// MATLAB Compiler: 3.0
// Date: Sat Feb 28 16:17:54 2004
// Arguments: "-B" "macro_default" "-O" "all" "-O" "fold_scalar_mxarrays:on"
// "-O" "fold_non_scalar_mxarrays:on" "-O" "optimize_integer_for_loops:on" "-O"
// "array_indexing:on" "-O" "optimize_conditionals:on" "-M" "-silentsetup" "-d"
// "G:/MATLAB/work/Fourier/src" "-B" "cppsglcom:Fourier,Fourier,1.0" "-B"
// "csglcom:Fourier,Fourier,1.0" "-B" "sgl" "-m" "-W" "main" "-L" "C" "-t" "-T"
// "link:exe" "-h" "libmmfile.mlib" "-W" "mainhg" "libmwsglm.mlib" "-t" "-W"
// "comhg:Fourier,Fourier,1.0" "-T" "link:lib" "-h" "libmmfile.mlib" "-i" "-L"
// "cpp" "-i" "G:/MATLAB/work/AddNoise.m" "G:/MATLAB/work/PowerSpectrum.m" 
//
#include "powerspectrum.hpp"
#include "libmatlbm.hpp"
#include "subplot.hpp"
#include "title.hpp"
#include "xlabel.hpp"

extern mwArray y;
static mwArray _mxarray0_ = mclInitializeDoubleVector(0, 0, (double *)NULL);
static mwArray _mxarray1_ = mclInitializeDouble(1000.0);
static mwArray _mxarray2_ = mclInitializeDouble(0.0);
static mwArray _mxarray3_ = mclInitializeDouble(1.0);
static mwArray _mxarray4_ = mclInitializeDouble(256.0);
static mwArray _mxarray5_ = mclInitializeDouble(512.0);
static mwArray _mxarray6_ = mclInitializeDouble(212.0);
static mwArray _mxarray7_ = mclInitializeDouble(257.0);

static mxChar _array9_[22] = { 'F', 'r', 'e', 'q', 'u', 'e', 'n', 'c',
                               'y', ' ', 'c', 'o', 'n', 't', 'e', 'n',
                               't', ' ', 'o', 'f', ' ', 'y' };
static mwArray _mxarray8_ = mclInitializeString(22, _array9_);

static mxChar _array11_[14] = { 'f', 'r', 'e', 'q', 'u', 'e', 'n',
                                'c', 'y', ' ', '(', 'H', 'z', ')' };
static mwArray _mxarray10_ = mclInitializeString(14, _array11_);

void InitializeModule_powerspectrum() {
}

void TerminateModule_powerspectrum() {
}

static mwArray Mpowerspectrum(int nargout_);

_mexLocalFunctionTable _local_function_table_powerspectrum
  = { 0, (mexFunctionTableEntry *)NULL };

//
// The function "powerspectrum" contains the normal interface for the
// "powerspectrum" M-function from file "g:\matlab\work\powerspectrum.m" (lines
// 1-17). This function processes any input arguments and passes them to the
// implementation version of the function, appearing above.
//
mwArray powerspectrum() {
    int nargout = 1;
    mwArray m = mwArray::UNDEFINED;
    m = Mpowerspectrum(nargout);
    return m;
}

//
// The function "mlxPowerspectrum" contains the feval interface for the
// "powerspectrum" M-function from file "g:\matlab\work\powerspectrum.m" (lines
// 1-17). The feval function calls the implementation version of powerspectrum
// through this function. This function processes any input arguments and
// passes them to the implementation version of the function, appearing above.
//
void mlxPowerspectrum(int nlhs, mxArray * plhs[], int nrhs, mxArray * prhs[]) {
    MW_BEGIN_MLX();
    {
        mwArray mplhs[1];
        mclCppUndefineArrays(1, mplhs);
        if (nlhs > 1) {
            error(
              mwVarargin(
                mwArray(
                  "Run-time Error: File: powerspectrum Line: 1 Column"
                  ": 1 The function \"powerspectrum\" was called with"
                  " more than the declared number of outputs (1).")));
        }
        if (nrhs > 0) {
            error(
              mwVarargin(
                mwArray(
                  "Run-time Error: File: powerspectrum Line: 1 Column"
                  ": 1 The function \"powerspectrum\" was called with"
                  " more than the declared number of inputs (0).")));
        }
        mplhs[0] = Mpowerspectrum(nlhs);
        plhs[0] = mplhs[0].FreezeData();
    }
    MW_END_MLX();
}

//
// The function "Mpowerspectrum" is the implementation version of the
// "powerspectrum" M-function from file "g:\matlab\work\powerspectrum.m" (lines
// 1-17). It contains the actual compiled code for that M-function. It is a
// static function and must only be called from one of the interface functions,
// appearing below.
//
//
// function [m]=PowerSpectrum()
//
static mwArray Mpowerspectrum(int nargout_) {
    mwLocalFunctionTable save_local_function_table_
      = &_local_function_table_powerspectrum;
    mwArray m = mwArray::UNDEFINED;
    mwArray Pyy = mwArray::UNDEFINED;
    mwArray Y = mwArray::UNDEFINED;
    mwArray f = mwArray::UNDEFINED;
    mwArray ans = mwArray::UNDEFINED;
    //
    // global x;       %Original Signal
    // global y;       %Noisy Signal
    // 
    // if (isempty(y))
    //
    if (tobool(isempty(mwVv(y, "y")))) {
        //
        // m = [];
        //
        m = _mxarray0_;
        //
        // return;
        //
        goto return_;
    //
    // end
    //
    }
    //
    // 
    // f = 1000*(0:256)/512;
    //
    f = _mxarray1_ * colon(_mxarray2_, _mxarray3_, _mxarray4_) / _mxarray5_;
    //
    // Y = fft(y,512);
    //
    Y = fft(mwVv(y, "y"), _mxarray5_);
    //
    // Pyy = Y.* conj(Y) / 512;
    //
    Pyy = times(mwVv(Y, "Y"), conj(mwVv(Y, "Y"))) / _mxarray5_;
    //
    // subplot(212)
    //
    ans.EqPrintAns(Nsubplot(0, _mxarray6_));
    //
    // plot(f,Pyy(1:257))
    //
    ans.EqPrintAns(
      Nplot(
        0,
        mwVarargin(
          mwVv(f, "f"),
          mclArrayRef(mwVv(Pyy, "Pyy"), colon(_mxarray3_, _mxarray7_)))));
    //
    // title('Frequency content of y')
    //
    ans.EqPrintAns(Ntitle(0, _mxarray8_, mwVarargin()));
    //
    // xlabel('frequency (Hz)')
    //
    ans.EqPrintAns(Nxlabel(0, _mxarray10_, mwVarargin()));
    //
    // m=Pyy;
    //
    m = mwVv(Pyy, "Pyy");
    return_:
    mwValidateOutput(m, 1, nargout_, "m", "powerspectrum");
    return m;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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.

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