How can I call MATLAB functions directly from Mathematica and transfer data/variables between the two systems?
4 Answers
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:
[]9
(source: matlink.org)
Disclosure: I am one of the developers of MATLink.
Installation
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:
SystemOpen@FileNameJoin[{$UserBaseDirectory, "Applications"}]
Then make sure you follow the operating system specific instructions described under "Link with MATLAB" on the home page.
Using MATLink
Load MATLink by evaluating
Needs["MATLink`"]
and launch MATLAB using
OpenMATLAB[]
This will launch a new MATLAB process in the background that Mathematica can communicate with.
To evaluate arbitrary MATLAB commands, use MEvaluate
. The output will be returned as a string.
MEvaluate["magic(4)"]
(* ==>
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
*)
To transfer data to MATLAB, use MSet
:
MSet["x", Range[10]]
MEvaluate["x"]
(* ==>
x =
1 2 3 4 5 6 7 8 9 10
*)
To transfer data back, use MGet
:
MGet["x"]
(* ==> {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} *)
Many data types are supported, including sparse arrays, struct
s and cell
s.
MATLAB functions can be wrapped using MFunction
and called directly from Mathematica:
eig = MFunction["eig"]
eig[{{1, 2}, {3, 1}}]
(* ==> {{3.44949}, {-1.44949}} *)
See the docs for more advanced usage and other functionality.
Simple examples
Plot the membrane from MATLAB's logo in Mathematica and manipulate the vibration modes:
Manipulate[
ListPlot3D@MFunction["membrane"][k],
{k, 1, 12, 1}
]
A bucky ball straight from MATLAB:
AdjacencyGraph@Round@MFunction["bucky"][]
Show Mathematica data in a zoomable MATLAB figure window:
mlf = LibraryFunctionLoad["demo_numerical", "mandelbrot", {Complex}, Integer];
mandel = Table[mlf[x + I y], {y, -1.25, 1.25, .002}, {x, -2., 0.5, .002}];
MFunction["image", "Output" -> False][mandel]
See the webpage for a few more complex examples.
Bugs and problems: If you find any, please do report them in email (matlink.m at gmail), on GitHub, or by commenting on this post. A support chatroom is also available.
-
-
1
-
3$\begingroup$ I am using MATLink to drive the Data Acquisition Toolbox on a test system I am developing. I would rather work entirely in Mathematica, but a) I don't believe there is a straightforward way to operate data acquisition systems from Mathematica and b) this approach lets me recycle another engineer's MATLAB code and get up to speed faster. Thanks for the package. $\endgroup$– Daniel WNov 24, 2013 at 13:35
-
$\begingroup$ I use MATLink to diagonalize large matrices (>10^4*10^4) and then process the result in MMA. Sometimes I also use the Spectrogram from Matlab. Thanks for making it so easy to use. $\endgroup$– Leo FangSep 10, 2014 at 3:16
-
$\begingroup$ One thing I wonder (not that I need it, just curious) is that do you plan to add the ability of handling arbitrary precision arithmetic to MATLink in the near future? $\endgroup$– Leo FangSep 10, 2014 at 3:18
Here I show the basic way to call MATLAB using NETLink
under Windows via the MATLAB COM interface. This answer is Community Wiki, feel free to extend it to others platforms and/or improve it!
In[1]:= Needs["NETLink`"]
matlab = CreateCOMObject["matlab.application"]
Out[2]= «NETObject[COMInterface[MLApp.DIMLApp]]»
Now one can invoke MATLAB functions:
In[4]:= matlab@Execute["version"]
Out[4]= "
ans =
7.9.0.529 (R2009b)
"
In[5]:= matlab@Execute["a=2"]
matlab@Execute["a*2"]
Out[5]= "
a =
2
"
Out[6]= "
ans =
4
"
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:
- execute arbitrary MATLAB commands and retrieve their output as a string
- transfer array variables between Mathematica and MATLAB
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 mEngine.sh
file needs to be edited and the MATLAB
variable set to the location of your MATLAB installation.
Note: If you get an error about not being able to start MATLAB, please first check that the PATH
variable in mEngine.sh
is set to point to MATLAB's bin
directory (which is inside the .app bundle). If it still doesn't work, please try to change the engOpen(NULL)
call in open.c
to engOpen("/path/to/matlab")
(of course substituting the correct path for your system; it should look similar to /Applications/MATLAB.app/bin/matlab
, but it'll depend on where you installed MATLAB and what you named it).
If it still doesn't work for you or if you find crashes, please leave a comment.
For what it's worth, there also seems to be a way to integrate Mathematica into matlab (the other way around) using the
"Mathematica Symbolic Toolbox for MATLAB--Version 2.0"
I have not personally tested this though, it seems quite old (2004, 2010).
-
$\begingroup$ This answer may be relevant here: mathematica.stackexchange.com/q/127452/12 $\endgroup$– SzabolcsOct 3, 2016 at 20:34
NETLink
on a Mac... $\endgroup$