Want to take your software engineering career to the next level? Join the mailing list for career tips & advice Click here


C++ convenience class to communicate with a Matlab instance. Send matrices back and forth, execute arbitrary Matlab commands, or drop into interactive Matlab session right in the middle of your C++ code.

Subscribe to updates I use MatlabWrapper

Statistics on MatlabWrapper

Number of watchers on Github 32
Number of open issues 1
Main language C++
Open pull requests 0+
Closed pull requests 0+
Last commit almost 8 years ago
Repo Created almost 8 years ago
Repo Last Updated over 2 years ago
Size 92 KB
Organization / Authorkarpathy
Page Updated
Do you use MatlabWrapper? Leave a review!
View MatlabWrapper activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Software engineers: It's time to get promoted. Starting NOW! Subscribe to my mailing list and I will equip you with tools, tips and actionable advice to grow in your career.
Evaluating MatlabWrapper for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)


Andrej Karpathy 27 August 2012

This C++ library is a convenience wrapper around Matlab Engine, especially if you use Eigen matrix library for working with matrices. It should be quite easy to adapt it to other library types as well.


Consider the simplest example: You have an Eigen::MatrixXf X; lying around and you'd love to run k-means on its rows. But naturally, you don't feel like re-implementing k-means for Eigen matrices from scratch! So you write:

  MatlabWrapper mw;
  mw.sendMat("X", X); // transfer it over to the engine under name "X"
  mw.exec("[IDX, C] = kmeans(X, 10);");
  Eigen::MatrixXf mat;
  mw.receiveMat("C", mat);

  // done!

That felt good. Now supose you've computing something complicated and would really like to explore it a bit interactively-- do a few plots, run some statistics, etc. No problem:

  MatlabWrapper mw;
  mw.sendMat("X", X);

This will start an interactive session with matlab right in your shell! You get to type whatever you wish:

>> plot(X(:,1), X(:,2)); // interesting!
>> close;
>> hist(X); 
>> close;

Entering qq will resume the c++ program right where it left off. This is quite great for debugging as well.


Eigen library is a dependency, and so is Matlab, naturally. If you don't have eigen, install it with

$ sudo apt-get install libeigen3-dev

Also make sure you have tcsh, which is require for matlab engine to work

sudo apt-get install tcsh

At this point you may want to get the official MATLAB demo to run (see below)

then to install this library.

$ git clone git@github.com:karpathy/MatlabWrapper.git 
$ cd MatlabWrapper
$ mkdir build 
$ cd build
$ cmake ..
$ make
$ ./test1

where last line runs a small demo. Naturally, the above will probably not run right away because these things never do. Have a look inside CMakeLists.txt and modify the include/library paths according to your system preferences. I tried to document it as best I could.

Getting Matlab Engine to run

If you haven't used Matlab Engine yet, try to get their demo running first. Here is the main documentation: http://www.mathworks.com/help/techdoc/matlab_external/f29148.html

In particular of interest is this link on getting their engdemo.c running: http://www.mathworks.com/help/techdoc/matlab_external/bsq776w-1.html#bsq78dr-9

If you can get that running, you're halfway there! Problems I ran across:

  • You must have tcsh installed! sudo apt-get install tcsh
  • I needed to add a missing .so file to my $LD_LIBRARY_PATH in .bashrc and source it.
  • They were looking for libc.so.6 in /lib64/ but mine was in /lib/x86_64-linux-gnu/ so I had to symlink it over.

Good luck!



MatlabWrapper open issues Ask a question     (View All Issues)
  • over 5 years features
MatlabWrapper list of languages used
Other projects in C++