Introduction

The project calculates the convolution of two vectors, and shows the resulting vector in the MsChart control. The function that performs the convolution is written in MatLab and then through the MatLab Add-in converted to C code ready to be used inside our app.

Requirements

Installing the MatLab Add-in

The add-in automates the integration of M-files into Visual C++ projects. The add-in for Visual Studio is automatically installed on your system when you run either mbuild -setup or mex -setup and select Microsoft Visual C/C++ version 6. In order to use the add-in you must follow these steps:

Start MatLab, in the prompt type mbuild -setup

Follow the menus and choose MS Visual C++6.0.

Type the following commands in the matlab prompt :

cd (prefdir)mccsavepath

These commands save the MatLab path to a file called mccpath in your user preferences directory (prefdir), ussually inside your documents and settings file. The path is used by the add-in because it runs outside Matlab and there is no other way for it to determine your Matlab path. If you add new directories to your Matlab path you will have to rerun this command if you want the add-in to see them.

Configure the Matlab Add-in for Visual Studio 6 to work within MSVisual C++.

Open MSVisualC++.

Select Tools -> Customize from the MSVC menu.

Click on the Add-ins and Macro Files tab.

Check MATLAB for Visual Studio on the Add-ins and Macro Files list and click Close.

The floating MATLAB add-in for Visual Studio toolbar appears. The checkmark directs MSVC to automatically load the add-in when you start MSVC again.

Calculating the convolution in Matlab

We will write a simple function in MatLab that will perform the convolution of two vectors:

In1 and In2 and store the result in out.

We actually can use any matlab built-in function or toolbox function inside our function here we use only the function conv for simplicity.

Writing the Application

Use the MFC AppWizard (exe) option to generate a Dialog Based Application and call it conv. Create a Button which will calculate the convolution of two given vectors.

Add the code below to the button's Message Handler.

//x1 and x2 are the input array to do the computation
//res will have the result, we already know that the length
//of the convolution of 2 vectors is (length(x1)+length(x2)-1)=19
double x1[]={1,2,3,4,5,6,7,8,9,0};
double x2[]={4,5,6,7,4,5,8,9,0,7};
double res[19];
//create the arrays that will be passed to MyFunc
mxArray* In1;
mxArray* In2;
mxArray* Out;
//we make them Real and have 10 values
In1=mxCreateDoubleMatrix(1,10,mxREAL);
In2=mxCreateDoubleMatrix(1,10,mxREAL);
//this make In1=x1;In2=x2
memcpy(mxGetPr(In1),x1,10*sizeof(double));
memcpy(mxGetPr(In2),x2,10*sizeof(double));
//Call to MyFunc.m that return the convolution of In1 and In2
Out=mlfMyFunc(In1,In2);
//now we have the result(Out) in a double array 'res'
memcpy(res,mxGetPr(Out),19*sizeof(double));
//Destroy matrices
mxDestroyArray(Out);
mxDestroyArray(In1);
mxDestroyArray(In2);

Copy MyFunc.m into your VC++ project directory. Now go to the MatLab Add-in and click the .m++ (Add m-files to current project) button:

Then select the MyFunc.m file and click Open. After a while you will see new files added to your project.

Under MATLAB M-files you will see our MyFunc.m, you can even edit it from inside Visual C++. Under MATLAB C/C++ are MyFunc.c and MyFunc_main.c, generated by the MatLab Compiler. And all the headers needed by the compiler.

We can't build the app yet, we must add the following lines to convDlg.cpp:

#include"matlab.h"#include"MyFunc.h"

Now the project should be built without any problem.

Points of Interest

The steps described here don't include the MSChart part, you can see it from the source code, but you can see that the calculation is done by placing a breakpoint inside the button message handler and checking the value of the resvector after the line:

Well I hope this simple article be interesting for MatLab lovers, this is the first article I wrote, so I apologize if it is not clear enough. I will be glad to explain anything if you ask me. Greetings.

Share

About the Author

Daniel Cespedes is now working on his final project to get the Electrical Engineering degree at the National University of Technology in Cordoba Argentina. He is developing a Computerized system for the study of Human Echolocation, the ability to detect obstacles with the echoes of self generated sounds.(yes like bats!!) at the CINTRA (Centro de Investigación y Transferencia Acústica).He uses MsVisual C++ 6 as a developing tool. He also work at the Software Research Lab at the University.
He comes from Sta.Cruz de la Sierra-Bolivia a paradise in SouthAmerica´s heart, where you can find pure air, nature contact, happy people, beautiful women etc.

I downloaded the files, and followed steps to install the add-in and build the project and it runs just fine. However, when I press the CONV button the application exits with a code of 1. When I debug the OnConv() this occurs at the mxCreateDoubleMatrix() call ... any ideas what is going on? Thanks!

I don´t understand very well what do you want to do.
you want to change the university you go now?? is that so?
which University do you want to go? Please give me more details so I can help you..
cheers!!

Daniel Cespedes

"There are 10 types of people, those who understand binary and those who do not""Santa Cruz de la Sierra Paraiso Terrenal!"

I have followed each and every step , when I open Myfunc.m file from MSVC after selecting Windows Console Exe .
ERROR Comes: MCC returned error code 1.
I have installed Matlab 6.5 , I don’t know whether Matlab C library is included with it or it is to be installed separately. I am doubtful that it may be the cause for this error.
When I build this application then following error comes:

I have followed each and every step , when I open Myfunc.m file from MSVC after selecting Windows Console Exe .
ERROR Comes: MCC returned error code 1.
I have installed Matlab 6.5 , I don’t know whether Matlab C library is included with it or it is to be installed separately. I am doubtful that it may be the cause for this error.
Please help me.
Thanks a lot .
M.Adil Hayat Khan

I have the little toolbar addin in VC++,and the M-file which runs from the Matlab "engine" in my debug directory.All was peachy till I tried to make a
shared dll,the mcc compiler crashed in my face.There has been no activity on this thread hope some1 takes a look soon.

Dear sir,
I read your replying in www.codeproject.com."Using MatLab Add-in for MS Visual Studio 6".I' m working with Matlab and convert m file to c++ file.I want to use c++ file to implement with MFC application.I don't know how I implement. I want to ask u how I bring matlab file(compile to c++) to implement with VC++ or u can show me some example to do that.For my application,I want to pass picture file name from VC++ to matlab c++ file and matlab process it and matlab pass picture that change to show in VC++.
Yours sincerely,
kritsadakorn K.

To:smsaam (irfanmughal@hotmail.com)
Now...I don't want to "how to addin for MFC". Because I think MATLAB can create Stand alone application and write program productivity.And It 's easy to create GUI by MATLAB.I think MATLAB 7 is supported all function that you want.Please try to learn how to use MATLAB,you will be succeed.

By the way ,if you really want to that I think "MATLAB Help" can help
you.

Dear Sir,
i wrote small m fucntion like this
function Out=RectLow()
w=boxcar(10+1);
pi=3.145678;
b = fir1(10,0.5/pi,w);
this is perfectly running in matlab editor but when i include this in vc++ project and collecting output in Mxarry it's not giving anyout piut.
i wasted full 3 hours time.many ways i tried.but i didn't find why it happend like that.
could u pls suggest me the solution
in vc++ after executing the memory address of b is always 0X0000000
why
thank u in advance
regards
usha

sir,
instead of matlab i used ellip and ellipord functions but it's not throwing any error compiling and not giving any result.
this is my m file
function out=MyEllipHigh(wp_n,ws_n,Atp_db,Ats_db,In1)
[n,wc_n]=ellipord(wp_n,ws_n,Atp_db,Ats_db);
[b1,a1]=ellip(n,wc_n,'high');
out= FILTER(b1,a1,In1);
here all input argumets are mxArry* and i checked it input are coming correctly when i kept breaking point at m file fucntion calling compiler just executing that state ment and coming out from debug mode with out giving output.
could u pls explain me why
regards

sir
when iam using matlab
in vc++ it is throwing this error
iam new to matlab so i am unable to find why this error is coming
when i compile it is giving this
Performing Custom Build Step on "e:\matlab6p5\toolbox\signal\signal\cheb2ap.m"
Error: "codege" is not a valid -T option argument.
Error executing c:\winnt\system32\cmd.exe.

sir,
i have the same doubt as u.i think u got solution.
pls sir could u pls provide me that solution it's very urgent for me.
i need to build standalone app which should run on other pc's which don't have matlab and vc++.iam new to both vc++ and matlab pls help me.it;s very importent for me