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.

10/20/07

Tutorial on MATLAB executables (MEX files)

Tutorial on MATLAB executables (MEX files)

How to Compile MATLAB C++ Math Library

How to Compile MATLAB C++ Math Library

How to Compile MATLAB C++ Math Library

by Munehiro Nakazato

RedPin Who need this?

  • (Scenario 1) If you need to write complicated matrix operations in C/C++
    You can save your time by using MATLAB Math Library.

  • (Scenario 2) If you want to call your own MATLAB functions from C/C++
    MATLAB compiler translates them into C++ codes.

  • (Scenario 3) If you need a speed boost of your MATLAB function or M-files
    You can use MATLAB compiler to translate your M-files into MEX-files which you can call from Matlab.
  • RedPin Manuals

    The manuals are available from MathWorks.
    Example codes are available in
    /usr/local/matlab6_R12/extern/examples/cppmath

    RedPin To Begin

    You need add specific library to your LD_LIBRARY_PATH

    Solaris: /usr/local/matlab6_12/extern/lib/sol2
    SGI 64 (such as guinan): /usr/local/matlab5.3.1/extern/lib/sgi64

    Example:

    # setenv LD_LIBRARY_PATH "/usr/local/matlab6_12/extern/lib/sol2:$LD_LIBRARY_PATH"

    RedPin Use MBUILD - the easiest way

    The easiest way to compile the code is mbuild tool

    When you run mbuild for the first time, I recommend you to setup mbuild

    # /usr/local/matlab6_R12/bin/mbuild -setup

    Then,

    # /usr/local/matlab6_R12/bin/mbuild ex1.cpp

    That's it !! It's very easy, isn't it?

    For Windows

    mbuild is in [matlab]\bin\mbuild, where [matlab] is the location of MATLAB instllation.

    RedPin Manual Compile

    For some situation, mbuild may not work well. In this case, you have to manually compile as shown below. The order to specify libraries is important.

    For Solaris

    As long as I know, MATLAB Math Library works only with Sun's C++ compilers (CC). It causes error with GNU C++ (g++)
    # CC -DSOL2 -DUNIX -DX11 -DNDEBUG -O3 +d ex1.cpp \
    -I/usr/local/matlab6_R12/extern/include/cpp \
    -I/usr/local/matlab6_R12/extern/include -L/usr/local/matlab6_R12/extern/lib/sol2 \
    -lmatpp -lmmfile -lmatlb -lmat -lmx -lnsl -lm

    For SGI 64 (guinan)

    # CC -64 -LANG:bool -DCOMPILER_HAS_BOOL ex1.cpp \
    -I/usr/local/matlab5.3.1/extern/include/cpp \
    -I/usr/local/matlab5.3.1/extern/include \
    -L/usr/local/matlab5.3.1/extern/lib/sgi64 \
    -lmatpp -lmmfile -lmcc -lmatlb -lmat -lmx -lm

    RedPin NOTE

    The directories in this documentation are specific to IFP Lab Workstations in Beckman Institute. You will need to change the file locations depending on your environment. Ask your System Administrator.

    10/8/07

    Ubuntu install matlab 2007.

    Have the following errors:

    -------------------------------------------------------------------

    An error status was returned by the program 'xsetup',
    the X Window System version of 'install'. The following
    messages were written to standard error:

    /data/matlab/update/install/main.sh: 168: /data/matlab/update/bin/glnx86/xsetup: Permission denied

    Attempt to fix the problem and try again. If X is not available
    or 'xsetup' cannot be made to work then try the terminal
    version of 'install' using the command:

    install* -t or INSTALL* -t

    -------------------------------------------------------------------

    Sorry! Setup aborted . . .

    To solve it:
    1. umount /cdrom
    2. mount -t iso9660 /dev/cdrom /cdrom
    3. /cdrom/install
    Now it will start. I don't know how to install matlab if I copy all the files to the hard disk.

    10/6/07

    Compile Qt with OpenGL support.

    When compile the source, make sure use the following to include opengl

    ./configure -opengl

    python problem.

    python configure.py
    from the latest PyQt4 snapshot, I got the following error message:
    Error: Failed to create ./qtdirs using platform linux-g++. Make sure your
    compiler is installed correctly and you have a working make on your PATH.

    use the following to solve problem.
    ln -s /usr/local/Trolltech/Qt.xx/bin/qmake /usr/bin/qmake

    export QTDIR = /usr/local/Trolltech/Qt.xxx/

    LIBS

    LIBS += -L/usr/local/lib/ -lqwtplot
    There is no space between L and /usr/local/lib.

    Enable and Disable Ubuntu Root Password -- Debian Admin

    Enable and Disable Ubuntu Root Password -- Debian Admin

    sudo” means superuser do. “sudo” will prompt for “Password:”. Please specify user password

    As you have noticed during the Ubuntu installation there was no question about the root password, as you might have been used to see during other Linux distribution installation process.Because of this your root accout is inactive.

    If you want to enable root account (which is not recommended) enter the following command.

    $sudo passwd root

    This will prompt for a new root password and once you confirm it, you can start using the root account to login.

    If you want to disable root account in ubuntu you need to lock the root account by using the following command

    $sudo passwd -l root

    If you want to work on a root console you’d better use the following command

    $sudo -i