You are on page 1of 5

A Tutorial to Call MATLAB Functions from Within A C/C++ Program

file:///C:/Computer/Studies/blind's%20paradise/c2matlab.htm

A Tutorial to Call MATLAB Functions from Within A C/C++ Program


Introduction
As the name suggest this tutorial is about calling MATLAB functions from a C/C++ files. Doing this has numerous advantages, mainly we could use the built-in commands and the toolbox command that MATLAB has without having to rewrite them in traditional C/C++ languages. We perform this using the MATLAB engine. The MATLAB Engine Library is a set of functions that allows you to call MATLAB from your own programs, thereby employing MATLAB as a computation engine. Doing this has numerous advantages, mainly we could use the built-in commands and the toolbox command that MATLAB has without having to rewrite them in traditional C/C++ languages. We do not have to write our own algorithms for things for example calculating the determinant or inverse of a matrix or in image processing applications such as edge detection, histogram analysis etc. In this tutorial we learn step by step how to write a C/C++ program to call MATLAB functions.

Software Used
Microsoft Visual C++ ver 6.0 MATLAB R12 Version 6

Knowledge Required
Basic Understanding of C/C++ programming ( Visual c++ is good) Basic Understanding of MATLAB Now lets get to the tutorial

Step 1
Start Microsoft Visual c++, choose New Project, choose a win32 application, (make sure the box in right hand bottom side) win32 is clicked. Name the project myFirstProject. You can choose an empty project. The window should look like this.

1 of 5

2/7/2008 6:22 PM

A Tutorial to Call MATLAB Functions from Within A C/C++ Program

file:///C:/Computer/Studies/blind's%20paradise/c2matlab.htm

Step2
Next we need to add paths to the MATLAB Engine for the header and the library files. For that click on tools -> options, choose directory. For the include files add new path i.e. where the MATLAB include directory is stored. It is usually in the MATLAB folder-> extern-> include. The window should look like this

Similarly add the paths for the library files. The path for the library files should in the MATLAB folder->extern->lib ->win32->Microsoft ->msvc6.0.

Step 3)
2 of 5 2/7/2008 6:22 PM

A Tutorial to Call MATLAB Functions from Within A C/C++ Program

file:///C:/Computer/Studies/blind's%20paradise/c2matlab.htm

Next click on Project -> Settings. In this click on link and under the object/library modules and add the following to it Libmx.lib libmex.lib libeng.lib The window should look like this

Now we are all done with the settings. Now lets start writing the code.

Step 4)
Choose File-> new -> a c++ source file. Name the file MyFirstFile. The code is all in BLUE. a) The header files need to written first #include<stdio.h> #include<stdlib.h> #includeengine.h

This file is needed to call the subroutines needed to call the MATLAB.

Next we need to declare the variables that we are going to use. Engine *ep; mxArray *mp, *ans; double *ar, *detreal; int i,j;

Step 5)
Then We create the matrix . The Matrix can be created in two ways: the first way is where we can create the Matrix in the MATLAB format right from the start using commands such as mxCreateDoubleArray. The

3 of 5

2/7/2008 6:22 PM

A Tutorial to Call MATLAB Functions from Within A C/C++ Program

file:///C:/Computer/Studies/blind's%20paradise/c2matlab.htm

second method being arbitrarily creating them and then converting them into the MATLAB format. One important thing to acknowledge is that the MATLAB processes the matrices column by column whereas a C stores the matrices row by row. Hence a transpose might be required. In this example we create a matrix right from beginning in the MATLAB format. mp = mxCreateDoubleMatrix(3,3,0); mxSetName(mp,"A"); ar = mxGetPr(mp); for( i =1;i<4:i++) { for (j = 1;j<4;:j++) { ar[i-1+3*(j-1)] = j*j*i*i; } }

Step 6)
Next we need to start the MATLAB Engine . for that we need to type in ep = engOpen(""); This command initializes the MATLAB engine and returns ep to it.

Step 7)
Next we need to put the Matrix that we created into the MATLAB Engine, That is done using the following command engPutArray(ep,mp);

Step8)
Now that our matrix is in MATLAB command window, we have to evaluate or call some of the MATLAB built-in functions, In this example lets evaluate the determinant engEvalString(ep,"d = det(A);");

Step 9) Since we are done with evaluating what we wanted to, we should now close the MATLAB engine and clear the Matrix using the following commands. engClose(ep);

Save the program, now you can compile and build it successfully.

A complete list of the commands can be found at the Mathworks website under external Interface. Downloads:

4 of 5

2/7/2008 6:22 PM

A Tutorial to Call MATLAB Functions from Within A C/C++ Program

file:///C:/Computer/Studies/blind's%20paradise/c2matlab.htm

The Complete Visual c++ project can be downloaded in zip form by click on this. MyFirstProject.zip

Conclusions
In this tutorial we learnt how to create a c program that would be able to call a simple MATLAB function such as Determinant. The programming can be taken further deep wherein we can use any built-in MATLAB function (from Image Processing Toolbox or from any other toolbox) and just call it in a C program. We need not rewrite the code again in C/C++. Hope you find this Tutorial Easy to Understand and follow. My Contact info Bharat Shah Email: bns23@drexel.edu

5 of 5

2/7/2008 6:22 PM

You might also like