Google
 

10/21/07

Calling Matlab functions from C++

It is very useful to call matlab functions from C++. For example, I can use C++ to do most of the calculations, and just use matlab functions to plot the results, or I can use Matlab to do some matrix manipulations. Here I illustrate how to do that under linux.

1. Matlab7.4.0 (2007a) must be installed.

2. MUST have gcc-4.1.1 compiler. It seems that gcc-4.1.2 doesn't work. Check here for the supported compilers. If your system doesn't have the right compiler, install one first. See the following tips for installing the compiler.

3. Check whether your system has csh or not. If you don't have it, install it.

4. In your .bashrc file, define the LD_LIBRARY_PATH
LD_LIBRARY_PATH=/home/xxx/program/matlab74/bin/glnx86:/home/
xxx/program/matlab74/sys/os/glnx86/:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

Also make sure that Matlab is in your path.
export PATH=/home/xxx/program/matlab74/bin/:$PATH
Note: Matlab is installed in /home/xxx/program/matlab74/ directory. You need to change according to your system.

5. Following the procedures in the matlab document. Compiling and Linking MATLAB Engine Programs . If everything works fine, this should give executable file "engdemo". Inside matlab if you want to run this you should use !./engdemo instead of the !engdemo.

6. Now you can also use the following to compile the "engdemo.c" in the linux console instead of inside matlab.
g++ -o engdemo engdemo.c -I
/home/xxx/program/matlab74/extern/include/ -L
/home/xxx/program/matlab74/bin/glnx86/ -leng -lm

Pay attention to the include path, and the lib path. Also there is the flag -leng.

Now you should be able to use the Matlab engine to call Matlab functions from c++. If you have some errors such as "matlab engine can not start" the chance is your compiler version is not the same one as matlab is built. You should use the exact same compiler as Matlab requests.
Here are some hints about setting up the right compiler.

Compiling and linking made difficult

Let's suppose we wanted to create an efficient MATLAB routine that calculates the height of the Normal probability density function (pdf) at a given point on the line. That there are at least five gazillion MATLAB implementations that already calculate this quantity is beside the point. (Normally, when you decide to write a MATLAB routine in C++, you should make sure it is worth your while!)

Before we embark on the actual C++ coding, we need to examine how MATLAB creates a MEX File. The exact procedure is system-dependent, so what I describe here may differ slightly from your setup. At school I have access to a machine installed with the Linux operating system, and I have my own Apple computer with Mac OS X. Both these operating systems are Unix-like, hence their differences will be rather cosmetic.

In order to build MEX files, we need to make sure that we have the proper compiler installed on our system. On my Linux machine, I'm using MATLAB 7.3 (R2006b), and according to MathWorks product support it was built with the GNU Compiler Collection (GCC) 3.4.5. Unfortunately, I did not have this particular version of the compiler installed on my system. Different versions of the same compiler are effectively different compilers, as they follow different conventions. You should never link libraries or object code unless they are compiled in the same way. I downloaded the GCC source from my local university FTP mirror and built the entire package on my system. GCC includes compilers for many different languages, and they can be found in the bin subdirectory of the GCC software installation. MATLAB uses three of them: the C compiler gcc, the C++ compiler g++, and the Fortran 77 compiler g77. When installing GCC, it is a bad idea to blindly follow the default installation as it may overwrite the existing compilers. You should install it in a new directory. Afterward, to make things simpler, I created a symbolic link to each of the compilers like so:

  cd /usr/bin
ln -s gcc-install-path/bin/gcc gcc-3.4.5

Then I did the same thing for the other two compilers. Since /usr/bin is in the path (see the environment variable PATH), I can call the program anywhere I am simply by typing gcc-3.4.5. By including the version number in the symbolic link, I avoid calling version 3.4.5 of gcc unless I really want to.

No comments: