How can I call MATLAB functions directly from Mathematica and transfer data/variables between the two systems?
2014-04-12 NOTICE: MATLAB R2014a contains a bug that breaks MATLink on OS X and Linux (Windows is fine). If you use MATLink on OS X or Linux, please consider keeping MATLAB R2013b until R2014b comes out. Due to the nature of the problem there is no quick workaround that we could apply in MATLink.
For full compatibility with Mathematica 10, please upgrade to MATLink 1.1.
Note: If you're using this package, please let us know how! Understanding how people use it helps us improve it in the right areas.
There is a new cross platform package for this, called MATLink. It allows calling MATLAB functions seamlessly, directly from Mathematica, as well as transferring data between the two systems. See below for a small tutorial:
Disclosure: I am one of the developers of MATLink.
Go to the MATLink home page and follow the instructions there. The simplest way is to download the archive and extract it to this directory:
Then make sure you follow the operating system specific instructions described under "Link with MATLAB" on the home page.
Load MATLink by evaluating
and launch MATLAB using
This will launch a new MATLAB process in the background that Mathematica can communicate with.
To evaluate arbitrary MATLAB commands, use
To transfer data to MATLAB, use
To transfer data back, use
Many data types are supported, including sparse arrays,
MATLAB functions can be wrapped using
See the docs for more advanced usage and other functionality.
Plot the membrane from MATLAB's logo in Mathematica and manipulate the vibration modes:
A bucky ball straight from MATLAB:
Show Mathematica data in a zoomable MATLAB figure window:
See the webpage for a few more complex examples.
Here I show the basic way to call MATLAB using
Now one can invoke MATLAB functions:
I'll leave this up on GitHub, but I won't maintain the port. I recommend using MATLink instead.
There's a package on the Wolfram Library Archive called mEngine that allows calling MATLAB from Mathematica. What it can do is:
It is mirroring the functionality of the "MATLAB Engine", MATLAB's C/Fortran interface.
This functionality can be used to set up Mathematica wrappers for MATLAB functions by transferring the arguments and return values through global variables. Ugly, yes, but it works and it's useful.
The original version of mEngine was written for Windows and comes with a precompiled executable that works on 32 bit systems. For other operating systems it need to be recompiled with modifications.
Using mEngine on OS X
Several modifications need to be made to mEngine before it can be compiled on OS X and it'll work on 64 bit systems. I put such a modified version on GitHub. Please get it from there and do not mix the files from this modified version with the original version found on the Wolfram Library Archive.
I uploaded a precompiled binary here. It should work on 64 bit OS X 10.6 or later. Important: When using this, the
Note: If you get an error about not being able to start MATLAB, please first check that the
If it still doesn't work for you or if you find crashes, please leave a comment.
protected by rm -rf♦ Nov 25 '13 at 6:55
Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?