Click here to Skip to main content
15,860,859 members
Articles / Desktop Programming / MFC
Article

Using MatLab Add-in for MS Visual Studio 6

Rate me:
Please Sign up or sign in to vote.
4.69/5 (22 votes)
4 Jul 2003CPOL3 min read 363.2K   3K   50   119
How to Use the MatLab Add-in for MSVC++ 6

Sample Image - MATLAB_ADD_IN.jpg

Introduction

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.

Requirements

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)
    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.

function out=MyFunc(In1,In2)
%Returns the convolution of vector IN1 and IN2
out=conv(In1,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
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:

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:

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

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


Written By
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

 
Generalmatlab and microsoft visual C++ Pin
sou.bhowmick29-Jun-10 3:29
sou.bhowmick29-Jun-10 3:29 
GeneralRe: matlab and microsoft visual C++ Pin
Cambalindo1-Jul-10 16:41
Cambalindo1-Jul-10 16:41 
QuestionPassing a 28x5700 double matrix from a C program to Matlab engine. Pin
Stefano7121-Sep-08 1:16
Stefano7121-Sep-08 1:16 
QuestionInvalid property value Pin
vitamine198323-May-08 20:28
vitamine198323-May-08 20:28 
AnswerRe: Invalid property value Pin
Cambalindo24-May-08 13:13
Cambalindo24-May-08 13:13 
GeneralRe: Invalid property value Pin
vitamine198325-May-08 10:52
vitamine198325-May-08 10:52 
GeneralRe: Invalid property value Pin
Cambalindo26-May-08 2:59
Cambalindo26-May-08 2:59 
GeneralError on the fatal error Pin
happy0121-May-08 17:46
happy0121-May-08 17:46 
GeneralRe: Error on the fatal error Pin
Cambalindo24-May-08 13:09
Cambalindo24-May-08 13:09 
AnswerANSWER TO SOME OF YOUR PROBLEMS Pin
leevonk13-May-08 9:46
leevonk13-May-08 9:46 
GeneralRe: ANSWER TO SOME OF YOUR PROBLEMS Pin
leevonk13-May-08 10:08
leevonk13-May-08 10:08 
AnswerRe: ANSWER TO SOME OF YOUR PROBLEMS Pin
leevonk13-May-08 11:01
leevonk13-May-08 11:01 
GeneralRe: ANSWER TO SOME OF YOUR PROBLEMS Pin
Cambalindo14-May-08 3:14
Cambalindo14-May-08 3:14 
Generalproblem Pin
ahmedY17-Feb-08 13:29
ahmedY17-Feb-08 13:29 
QuestionHow can I Pin
flomerie20-Jul-07 4:16
flomerie20-Jul-07 4:16 
AnswerRe: How can I Pin
Cambalindo2-Aug-07 7:18
Cambalindo2-Aug-07 7:18 
Generalconfuse Pin
cocojin26-Oct-06 20:33
cocojin26-Oct-06 20:33 
Questionconfigure the MATLAB add-in for VISUAL STUDIO Pin
Joel Karbassi17-Sep-06 6:20
Joel Karbassi17-Sep-06 6:20 
i follow the instruction to
Configure the Matlab Add-in for Visual Studio 6 to work within MSVisual C++:
by Open MSVisualC++, Select Tools -> Customize from the MSVC menu.
Clicking on the Add-ins and Macro Files tab.
but i dont see any MATLAB for Visual Studio on the Add-ins or Macro Files list ,
i also type the command in MAATLAB:
cd(prefdir) ;
save mccpath;

sincerely Joel
AnswerRe: configure the MATLAB add-in for VISUAL STUDIO Pin
Cambalindo29-Sep-06 10:05
Cambalindo29-Sep-06 10:05 
GeneralRe: configure the MATLAB add-in for VISUAL STUDIO Pin
RAJU PONNAGANTI2-Aug-14 10:35
RAJU PONNAGANTI2-Aug-14 10:35 
QuestionUndefined Function Errors Pin
UJ_Student16-May-06 1:49
UJ_Student16-May-06 1:49 
GeneralHELP: Errors Using MSVC and the MATLAB add-in Pin
UJ_Student16-May-06 0:51
UJ_Student16-May-06 0:51 
QuestionUsing VS .net with MatLab Add-in Pin
sillE maiL9-Mar-06 10:57
sillE maiL9-Mar-06 10:57 
Generalpiecewise Pin
Anonymous18-Oct-05 11:44
Anonymous18-Oct-05 11:44 
Generalpuedo usar archivos .mat usando add in Pin
Hector Villacorta17-Aug-05 7:18
sussHector Villacorta17-Aug-05 7:18 

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.