integrating ms visual studio c++ with matlab ankit bhurane

8
1 Although this tutorial is described for the specific version of the software, it should work for other versions too by tweaking few things. Step 1: Setup path Set the path for the required libraries. This can be done as follows. Right-click Computer Properties Advanced system settings Advanced Tab Environment Variables User variables for Path Add following paths (separated by semicolon ;) as shown below The path may of course change according to your Matlab installation folder. Typically it is C:\Program Files\MATLAB\R2012b Step 2: Start New Project Start a New Project with win32 console applications. Add demo files to the project in the Source. Step 3: Add files to the project with following code C:\Program Files\MATLAB\R2012b\runtime\win64; C:\Program Files\MATLAB\R2012b\bin; C:\Program Files\MATLAB\R2012b\bin\win64 Integrating MS Visual Studio C++ 2008 (Professional Edition) with Matlab (64 bit) in Windows 7 (64 bit) Ankit A. Bhurane ([email protected])

Upload: ankit-bhurane

Post on 16-Apr-2015

61 views

Category:

Documents


4 download

DESCRIPTION

From C++ to Matlab

TRANSCRIPT

Page 1: Integrating MS Visual Studio C++ With Matlab Ankit Bhurane

1

Although this tutorial is described for the specific version of the software, it should

work for other versions too by tweaking few things.

Step 1: Setup path

Set the path for the required libraries. This can be done as follows.

Right-click Computer Properties Advanced system settings Advanced Tab

Environment Variables User variables for Path Add following paths

(separated by semicolon ;) as shown below

The path may of course change according to your Matlab installation folder. Typically

it is C:\Program Files\MATLAB\R2012b

Step 2: Start New Project

Start a New Project with win32 console applications. Add demo files to

the project in the Source.

Step 3: Add files to the project with following code

C:\Program Files\MATLAB\R2012b\runtime\win64; C:\Program Files\MATLAB\R2012b\bin; C:\Program Files\MATLAB\R2012b\bin\win64

Integrating MS Visual Studio C++ 2008 (Professional

Edition) with Matlab (64 bit) in Windows 7 (64 bit)

Ankit A. Bhurane ([email protected])

Page 2: Integrating MS Visual Studio C++ With Matlab Ankit Bhurane

2

c2m.cpp (code taken from https://www.box.com/shared/xmazi14uv2)

#include "stdafx.h"

#include<iostream>

#include<cmath>

#include "engine.h"

using namespace std;

int main(){

Engine *ep = engOpen(NULL);

double x[1000];

double y[1000];

double z[1000];

double t = 0;

const double dt = 0.001;

int i,j;

double a,b;

mxArray *z_array = mxCreateDoubleMatrix(1000,1,mxREAL);

mxArray *a_array = mxCreateDoubleMatrix( 1,1,mxREAL);

mxArray *b_array = mxCreateDoubleMatrix( 1,1,mxREAL);

double *pz = mxGetPr(z_array);

double *pa = mxGetPr(a_array);

double *pb = mxGetPr(b_array);

for (i=0;i<1000;i++){

x[i] = cos(2*3.14*t);

y[i] = sin(2*3.14*t);

t+=dt;

}

a = 1;

b = 0;

for (j=0;j<100;j++){

for(i=0;i<1000;i++){

z[i] = a*x[i] + b*y[i];

pz[i] = z[i];

}

pa[0] = a;

pb[0] = b;

engPutVariable(ep,"z",z_array);

engPutVariable(ep,"a",a_array);

engPutVariable(ep,"b",b_array);

engEvalString(ep,"testPlot");

engEvalString(ep,"plot(z);");

engEvalString(ep,"axis([0 1000 -1 1]);");

engEvalString(ep,"grid on;");

engEvalString(ep,"title(sprintf('a = %0.3f \t b = %0.3f',a,b));");

engEvalString(ep,"pause(0.1);");

a = a - 0.01;

b = b + 0.01;

}

engClose(ep);

return 0;

}

Page 3: Integrating MS Visual Studio C++ With Matlab Ankit Bhurane

3

Step 4: Setup MS Visual Studio for 64-Bit application

Right-click Project Name Properties Configuration Manager (right-top)

Active solution platform x64

Note: If x64 option is absent, search in <New…>. If still unavailable, the 64-bit

compiler seems to be absent. Consider reinstallation of MS Visual Studio with Full

Installation mode.

Step 5: Add additional include directories

Right-click Project Name Properties C/C++ Additional Include Directories

C:\Program Files\MATLAB\R2012b\extern\include

Page 4: Integrating MS Visual Studio C++ With Matlab Ankit Bhurane

4

Step 6: Add additional library directories

Right-click Project Name Properties Linker Additional Library Directories

Step 7: Add additional library directories

Right-click Project Name Properties Linker Additional Library Directories

C:\Program Files\MATLAB\R2012b\extern\lib\win64\microsoft

libeng.lib libmat.lib libmx.lib

Page 5: Integrating MS Visual Studio C++ With Matlab Ankit Bhurane

5

Step 8: Build the Solution

Step 9: Start Debugging

Start debugging by putting a debugging point at engClose(ep);

The output will be shown in Matlab windows. For confirmation one can consider

displaying/ opening workspace by entering workspace in the Matlab Command

Window s shown in the figure.

Page 6: Integrating MS Visual Studio C++ With Matlab Ankit Bhurane

6

Page 7: Integrating MS Visual Studio C++ With Matlab Ankit Bhurane

7

Handling errors:

1) . pch file missing

Right-click Project Name Properties C/C++ Precompiled Headers Select Not Using Precompiled Headers

2) libeng.dll not found

Try setting Working Directory to

This can be done as follows

Right-click Project Name Properties Configuration Properties

Debugging Working Directory Edit the path

C:\Program Files\MATLAB\R2012b\bin\win64

Page 8: Integrating MS Visual Studio C++ With Matlab Ankit Bhurane

8

OR

Try setting Environment to

This can be done as follows

Right-click Project Name Properties Configuration Properties

Debugging Environment

Other examples are available inside Matlab folder

Example: Try building, engdemo.c, it should run!

References:

1. http://www.mathworks.in/support/solutions/en/data/1-

FWTSV5/index.html

2. http://michael-stengel.com/blog/?p=89

3. http://obeidlab.blogspot.in/2011/05/neat-coding-trick-2.html

4. In Linux: http://www.youtube.com/watch?v=3zdVQf1MotY

C:\Program Files\MATLAB\R2012b\bin\win64

C:\Program Files\MATLAB\R2012b\extern\examples\eng_mat