Click here to Skip to main content
14,238,485 members

Using MatLab Add-in for MS Visual Studio 6

Rate this:
4.69 (22 votes)
Please Sign up or sign in to vote.
4.69 (22 votes)
4 Jul 2003CPOL
How to Use the MatLab Add-in for MSVC++ 6

Sample Image - MATLAB_ADD_IN.jpg


Following the line of the article posted by A.Riazi, Solving engineering problem using MATLAB C API, I will show in this small example how to use M Functions inside our VC++ 6 project.

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.


You need Visual Studio 6 and MatLab Release 12 installed with the MatLab C Library and Matlab Compiler.

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)

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.

function out=MyFunc(In1,In2)
%Returns the convolution of vector IN1 and IN2

Save the function with the name MyFunc.m </FONT>

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

//this make In1=x1;In2=x2

//Call to MyFunc.m that return the convolution of In1 and In2

//now we have the result(Out) in a double array 'res'

//Destroy matrices

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:

Sample screenshot

Select Windows Console Exe from the Combo Box and check Generate main file and debug mode. Press OK.

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

Sample screenshot

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 res vector 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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Motorola
United States United States
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.

Comments and Discussions

Generalcannot call imread function Pin
Caillou15-Sep-03 4:44
memberCaillou15-Sep-03 4:44 
QuestionWhat about Image processing Toolbox Pin
Caillou5-Sep-03 6:40
memberCaillou5-Sep-03 6:40 
GeneralCannot open file &quot;libmmfile.lib&quot; Pin
Teo Gian29-Aug-03 2:46
memberTeo Gian29-Aug-03 2:46 
GeneralRe: Cannot open file &quot;libmmfile.lib&quot; Pin
Cambalindo1-Sep-03 9:36
memberCambalindo1-Sep-03 9:36 
GeneralI don't see convolution Pin
Christiantc123-Aug-03 12:07
memberChristiantc123-Aug-03 12:07 
GeneralRe: I don't see convolution Pin
Cambalindo24-Aug-03 9:10
memberCambalindo24-Aug-03 9:10 
Generalwhy it is so slow to use matlab add-in Pin
mrpyu11-Aug-03 21:18
membermrpyu11-Aug-03 21:18 
I have written a c++ program with matlab add-in. But it is extremely slow when I run it.
Is there anything wrong?
GeneralError: Can't locate in @INC Pin
JLP18-Aug-03 8:58
sussJLP18-Aug-03 8:58 
Generali can't run Pin
suma santhosh7-Aug-03 20:36
membersuma santhosh7-Aug-03 20:36 
GeneralRe: i can't run Pin
Cambalindo8-Aug-03 7:11
memberCambalindo8-Aug-03 7:11 
GeneralDLL Static Build Pin
Member 4251028-Jul-03 8:03
memberMember 4251028-Jul-03 8:03 
GeneralRe: DLL Static Build Pin
Cambalindo11-Jul-03 12:19
memberCambalindo11-Jul-03 12:19 
GeneralTo call a matlab generated dll from visual basic Pin
tin_maria7-Jul-03 19:27
membertin_maria7-Jul-03 19:27 
GeneralRe: To call a matlab generated dll from visual basic Pin
Cambalindo10-Jul-03 13:09
memberCambalindo10-Jul-03 13:09 
QuestionHow to make Static Build ? Pin
EssamZaky1-Jul-03 23:11
memberEssamZaky1-Jul-03 23:11 
AnswerRe: How to make Static Build ? Pin
Cambalindo2-Jul-03 5:31
memberCambalindo2-Jul-03 5:31 
GeneralRe: How to make Static Build ? Pin
EssamZaky2-Jul-03 23:54
memberEssamZaky2-Jul-03 23:54 
GeneralRe: How to make Static Build ? Pin
Cambalindo3-Jul-03 9:02
memberCambalindo3-Jul-03 9:02 
GeneralMatlab Graphs via Dlls Pin
Member 42510226-Jun-03 5:11
memberMember 42510226-Jun-03 5:11 
GeneralRe: Matlab Graphs via Dlls Pin
Cambalindo26-Jun-03 14:16
memberCambalindo26-Jun-03 14:16 
GeneralRe: Matlab Graphs via Dlls Pin
Member 4251028-Jul-03 8:10
memberMember 4251028-Jul-03 8:10 
GeneralRe: Matlab Graphs via Dlls Pin
Cambalindo11-Jul-03 12:08
memberCambalindo11-Jul-03 12:08 
Questioncan add-in be used for bp neural work? Pin
hi-linfeng17-Jun-03 22:13
memberhi-linfeng17-Jun-03 22:13 
AnswerRe: can add-in be used for bp neural work? Pin
Cambalindo18-Jun-03 4:28
memberCambalindo18-Jun-03 4:28 
Generalthanks a lot,could you help me to watch my code ? Pin
hi-linfeng18-Jun-03 15:13
memberhi-linfeng18-Jun-03 15:13 

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.

Posted 24 May 2003


50 bookmarked