Using Matlab from Prolog

Note: This page describes experimental code that has not been used in a while. Please try it at your own risk.

The Matlab-Prolog interface PLML allows to call Matlab functions from Prolog. The following steps describe the setup as it worked in our lab. You will need to adapt the paths to your setup and possibly change other values if you have a different configuration.

You need to set the following values in the Makefile (adapt the pa:

export MATLAB=/usr/local/lehrstuhl/DIR/matlab-2007b
export MLARCH=glnxa64
export MLLIBS=-leng -lmx -lmat -licuuc -licudata -licui18n -lz -lreadline
export SO=so
export INCLUDES=
export PLLD=/usr/local/lehrstuhl/DIR/pl/lib/pl/bin/x86_64-linux/plld
export INSTALL_LIB_TO=~/work/owl/lib
export INSTALL_PL_TO=~/work/owl/lib
export INSTALL_FLAGS='-bp'

Generate a new file and call it: setPathMatlab.m this file should have the following:


Then move/copy the new file “setPathMatlab.m” to the following address: ~/work/owl

Enter the file plml.cpp and add the following (~line 179)

if (ep) {

    engEvalString(ep, "setPathMatlab;"); //add this to set the path in Matlab
    outbuf=new char[BUFSIZE];

Configure, make, make install. Set the library path to include the MATLAB directories

export LD_LIBRARY_PATH=/usr/local/lehrstuhl/DIR/matlab-2007b/bin/glnxa64:$LD_LIBRARY_PATH

Move to the ~/work/owl directory, start Prolog and run the following commands

?- assert(user:file_search_path(foreign, '/usr/wiss/tenorth/work/owl/lib')), 
   assert(user:file_search_path(library, '/usr/wiss/tenorth/work/owl/lib')).

?- use_module(library(plml)).
% hostname set to atradig131
%  library(hostname) compiled into hostname 0.03 sec, 403,152 bytes
%   library(ops) compiled into ops 0.00 sec, 8,104 bytes
%  library(utils) compiled into utils 0.01 sec, 65,408 bytes
%  library(flists) compiled into flists 0.00 sec, 10,456 bytes
% library(plml) compiled into plml 0.08 sec, 619,872 bytes

?- ml_open(ml).
Matlab engine (ml) open.

?- float(A)===2*pi.
A = 6.28319.

NOTE: In case you have the following error:

?- ml_open(ml).
ERROR: Unhandled exception: Unknown message: open engine failed

This means that you need to do the following:

$ sudo apt-get install csh. 

By default c-shell is installed in /bin/csh. That is necessary because you need to have C shell installed in your system as /bin/csh for the matlab engine program to function correctly. You can use any shell, but just that C shell has to be there in the system so that the engine program can use it.