A Tutorial To Call MATLAB Functions From Within A C C++ Program
A Tutorial To Call MATLAB Functions From Within A C C++ Program
file:///C:/Computer/Studies/blind's%20paradise/c2matlab.htm
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
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
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
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
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: [email protected]
5 of 5
2/7/2008 6:22 PM