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:

memcpy(res,mxGetPr(Out),19*sizeof(double));

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.

License

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 think the problem is that mexsetup.pm is in C:\Apps\MATLAB6p5\bin\win32. The path shown is missing the "apps" part plus it's intermixing forward slashes and backward slashes. How can I change the path where it's searching for mexsetup.pm?

Hi Suma, tell me what you did.
did you downloaded the zip file or did you typed the code from the example.
If you did the latter it does not include the mschart part, see the Points of Interest part in the page.
If you downloaded the code, tell me what error you got.

Daniel Cespedes

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

Hey Mate,
I've been trying to get my dll to run on a computer server, which does not have matlab or MSVS 6.0 installed. I used the packager and created a self-extracting .exe application for my Windows Console EXE dll.I've copied my mgl_runtime_dir\bin\win32 directory into my system PATH directory. I am using Windows 2000 Advanced server operating system. I really can’t understand why this doesn't work. I'm absolutely sure that it has to be related to the PATH directories not accessing Matlab's runtime
directories.
I have the dll working perfectly on the machine that it was developed on, which obviously has Matlab and Visual Studio installed. If I remove the "c:\matlab6p5\bin\win32" path from the system PATH I receive the exact same error messages as my server machine. My development computer uses Windows 2000. Could this be a problem related to Windows 2000 Advanced Server?

Hi,
Try putting your app in the mgl_runtime_dir\bin\win32 directory to check if it works.
If it works there is a problem with the path in your server.
If it does not work, sorry have no idea what´s happening
cheers mate!

Daniel Cespedes

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

Is there any method by which i can call a matlab generated dll from Visual Basic. I tried different methods like creating an OCX component and all.Bu it not working.My need is as follows:

I have a matlab generated dll to which i have to pass a file name as a parameter. I have to call this dll from a visual basic application. I have read many threads that you have replied on calling matlab from VC. I wuill very grateful if you can help me on this as early as possible.

Hi:
This program runs only in machines with Matlab and VC installed.
However,to run this program or any program generated with the Matlab Add-in for MsVC you have to package your app with a file called: mglinstaller.exe located in yourMatlabInstallation\extern\lib\win32.
This file has all the MATLAB run-time libraries required by stand-alone applications.
To install your application, your customers must:
•Run the MATLAB Math and Graphics Run-Time Library Installer(mglinstaller.exe). This program extracts the libraries from the archive and installs them in subdirectories of a directory specified by the user.
•Add the bin/Win32 subdirectory to their path. This is the only MATLAB
Math and Graphics Run-time library subdirectory that needs to be added to
the path.
For example, if your customer specifies the installation directorymgl_runtime_dir, then he must add mgl_runtime_dir\bin\win32 to PATH.
The bad news is that this file increments the size of your app in 18Mb!!
the mglinstaller.exe has 7Mb.
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!"

Hey Daniel, how ya going mate?
Just a quick question. I'm trying to create a Dll that will plot x[100] vs. y[100] via matlab. I simply want to input the 2 arrays and have the Dll plot them using the matlab plotting functions. I have tried this using the same method I used to create my Press2_Energy dll with except for clicking on the "Use Handle Graphic Libraries" box when selecting my M-file. When I run my dll it simply hangs up so I'm not sure if I'm doing something wrong. Any help would he greatly appreciated. Thanks again mate for your continued help.

Hi, Yes I´m having the same problem, I never used the graphic library from within VC++ I always generate stand alone apps from matlab when I want to use graphic function. We should ask Mathworks whats happening.
If you want to try generating the full exe from matlab use this:
suppose you have this m. file:

Hey,
I spoke to the blokes at the Matlab hotline and they say it has never been tested with a dll. It works fine with a .exe but they recommend creating a COM object. Also check out Solution Number 27388 in the matworks FAQ @ www.matworks.com. It seems to have something relating to this conundrum.

hello ,thanks for your excellent paper.
can add-in be used for bp neural work follow the example of yours?
these days ,i try to use the function of simuff() of matlab in msvc by add-in,i can complie the project but can not get the results.
when i use the breakpoint to debug it,the errors take place as follows:
Loaded 'C:\WINNT\System32\ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\user32.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\kernel32.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\gdi32.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\libmmfile.dll', no matching symbolic information found.
......

Hi, where did you put the breakpoint, put it after the call to simuff and check the results in the Variables Window (Alt+4), what you point as errors is just information of the libraries loaded by the app.
regards

thanks a lot for your reply,could you help me to watch my code,it is just a very simple project,i only want to get the results of simuff in VC by click the button.thanks a lot,i have send my code to your e-mail.

Hi...
I´ve checked your m code, and first of all, your function and your m file must have the same name (you called your file bptrain.m and your function trainbp).
Second, the functions you called from bptrain.m use matlab graphics functions.
When you add your m function to your VC++ project you have to check the Use Handle Graphics Libraries checkbox.
Now your project should work.
cheers!

hi,thanks for your help very much.i am very sorry that i have sent the bptrain.m to you ,while i have decided to send the bpnet.m to you.(in the bpnet.m the functin is bpnet()).i try again follow your advise and add bpnet.m to my project with checking the Use Handle Graphics Libraries checkbox.My project can work but it gets nothing.(it may gets three numbers as expected).i send bpnet.m to you again,please read it ,thanks a lot,i am very sorry to bother you again.

Hi mate!!
I´ve been checking your code, and it seems that you are using obsolete functions of the nn toolbox. I got the following messages:
*WARNING* SIMUFF is an obsolete function.
Use NNT2FF and SIM to update and simulate your network.
Type NNTWARN OFF to suppress NNT warning messages.

*WARNING* TANSIG used in an obsolete way.
Use TANSIG(NETSUM(Z,B)) instead of TANSIG(Z,B).
Type NNTWARN OFF to suppress NNT warning messages.

*WARNING* PURELIN used in an obsolete way.
Use PURELIN(NETSUM(Z,B*ones(1,Q))) instead of PURELIN(Z,B).
Type NNTWARN OFF to suppress NNT warning messages.

why don´t you try using the new functions that replace the obsolete ones.
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!"

hi,thanks for you help,i have tried it again ,i have used net=nnt2ff(),sim(net,in) to replace the old ones,but i get the news as follows.i think perhaps add-in does not support the programming of bp network and it only can be used for matrix operation.Perhaps i have to solute the problem by matcom or engin API.Thanks for you help very much.
This is the output from running MATLAB Add-in.
Please close this window after viewing it.
--------------MCC/MEX BEGIN---------------
mcc -k "D:\addinlizi\bpnet2222\mcc.mak" -/n -t -W mex -T link:mexlibrary -L C -A line:on -g libmatlbmx.mlib -vh "D:\bpnet22.m"
Warning: File: newff Line: 167 Column: 7
The MATLAB Compiler does not currently support MATLAB object-oriented programming. References to the method "init" will produce a run-time error.
Warning: File: newff Line: 133 Column: 7
The MATLAB Compiler does not currently support MATLAB object-oriented programming. References to the method "network" will produce a run-time error.
Compiler version: 3.0