How can I call MATLAB functions directly from Mathematica and transfer data/variables between the two systems?
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:
Disclaimer: 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.
Bugs and problems: If you find any, please do report them on GitHub, in email, or by commenting on this post.
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.
Here I show the basic way to call MATLAB using
Now one can invoke MATLAB functions: