Click here to Skip to main content
Click here to Skip to main content

Using MatLab Add-in for MS Visual Studio 6

By , 4 Jul 2003
 

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

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)

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalmatlab and microsoft visual C++membersou.bhowmick29 Jun '10 - 3:29 
please tell me how can i use matlab functions in msvc while i am using matlab 7(r14) and msvc 6.0............
GeneralRe: matlab and microsoft visual C++memberCambalindo1 Jul '10 - 16:41 
HI,
Please check here http://www.codeproject.com/Messages/2551239/ANSWER-TO-SOME-OF-YOUR-PROBLEMS.aspx[^] you may find the answer you are looking for.
let me know if it helps.
thanks.
Daniel Cespedes
 

daniel.cespedes@ieee.org

QuestionPassing a 28x5700 double matrix from a C program to Matlab engine.memberStefano7121 Sep '08 - 1:16 
Hi,
I have written a traffic simulator based on C language, and I launch it using the Matlab engine functionality after compiling it with LCC.
Everything works well as long as I only pass single double variable from the simulator to the Matlab workspace. But I can't find a way to pass the whole Matrix, which is 28x5700.
I could not adapt the examples I have read here (using memcpy), probably I miss some background on this topic.
I have been striving against this for some weeks and any help will be very appreciated.
Thank you
Stefano.
QuestionInvalid property valuemembervitamine198323 May '08 - 20:28 
Hello Cambalindo Smile | :)
Thank you for the perfect project and beautipfu explanation. Smile | :) Iwell compiled your project, but when I start it and when I press "CONV" button the Error box appears which is saying about "Invalid property value".
How do I can reapare that ?Confused | :confused:
 
That message appears in WINCORE.CPP file in such place
 
CATCH_ALL(e)
{
CWinThread* pWinThread = AfxGetThread();
if ( pWinThread != NULL )
{
lResult = pWinThread->ProcessWndProcException(e, &pThreadState->m_lastSentMsg);
TRACE1("Warning: Uncaught exception in WindowProc (returning %ld).\n",
lResult);
}
else
{
TRACE0("Warning: Uncaught exception in WindowProc.\n");
lResult = 0;
}
DELETE_EXCEPTION(e);
}
 
when is executing this code part in CONV button function
if(n<11)
{
m_Chart.SetColumn(1);
m_Chart.SetRow(n+1);
value=do2CStr(x1[n]);
m_Chart.SetData(value);
 
m_Chart.SetColumn(2);
m_Chart.SetRow(n+1);
value=do2CStr(x2[n]);
m_Chart.SetData(value);
 
}
AnswerRe: Invalid property valuememberCambalindo24 May '08 - 13:13 
Hi,vitamine, thanks for your words regarding the article.
It's really annoying that it's not working for you, and unfortunately I don't have an answer for your error, could you tell me what Matlab and VS versions are you using?
regards.
 
Daniel Cespedes
 
"There are 10 types of people, those who understand binary and those who do not"
"Santa Cruz de la Sierra Paraiso Terrenal!"
 
daniel.cespedes@ieee.org

GeneralRe: Invalid property valuemembervitamine198325 May '08 - 10:52 
MATLAB version is 6.5 or VS according to the article - 6.0. I gess that problem is with visualisation, because in initialisation of graph window defined 10 points (input data), after convolition we get 16 points. In my opinion is something wrong with reinitialisation.
GeneralRe: Invalid property valuememberCambalindo26 May '08 - 2:59 
perhaps, you could try without the mschart, comment all the lines corresponding to the chart, and see if your function is working. If it works, the problem es in the chart part, I'll review it to see if I can find the error.
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!"
 
daniel.cespedes@ieee.org

GeneralError on the fatal errormemberhappy0121 May '08 - 17:46 
Hi,
 
Could you give me idea on this errors? I am currently using Matlab 7.5.0(2007b) and Microsoft Visual c++ 6.
 
Please. thank alot.
 
c:\documents and settings\administrator\desktop\matlab_add_in\myfunc.h(21) : fatal error C1083: Cannot open include file: 'libmatlb.h': No such file or directory
MyFunc_main.c
c:\documents and settings\administrator\desktop\matlab_add_in\myfunc_main.c(14) : fatal error C1083: Cannot open include file: 'libmatlb.h': No such file or directory
Error executing cl.exe.
Build : warning : failed to (or don't know how to) build 'E:\trabajos\daniel\practicasC++\conv\MyFunc.m'
GeneralRe: Error on the fatal errormemberCambalindo24 May '08 - 13:09 
Hi, pls. see the thread right below this one, I think your errors are addressed there.
another thing:
the last warning, is addressing my HardDisk:
Error executing cl.exe.
Build : warning : failed to (or don't know how to) build 'E:\trabajos\daniel\practicasC++\conv\MyFunc.m' 
check that should be your path to your function.
regards
 
Daniel Cespedes
 
"There are 10 types of people, those who understand binary and those who do not"
"Santa Cruz de la Sierra Paraiso Terrenal!"
 
daniel.cespedes@ieee.org

AnswerANSWER TO SOME OF YOUR PROBLEMSmemberleevonk13 May '08 - 9:46 
I had matlab 7.0 and the mccsavepath command doesn't work anymore, this is because this version of matlab supposedly 'automatically' adds its path to the windows path. this is not necessarily done right and can be corrected manually. See instructions here and add you matlab bin directory to the path, for example on my computer I copied this to my path:
C:\Program Files\MATLAB\R2007a\bin
C:\Program Files\MATLAB\R2007a\bin\win32
 
also, make sure your visual c++ bin directory is in the same path, on my computer:
C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin
 
Before I discovered this I reinstalled an older version of matlab that did have a working mccsavepath command, matlab 6.5 (release 13). in matlab 6.5 I typed in the commands as this article shows and they worked ok, but then I still had to go into the windows path and manually make sure that the matlab and vc++ bin directories were in the windows path:
c:\matlab6p5\bin\win32
C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 5 Jul 2003
Article Copyright 2003 by Cambalindo
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid