Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How can I call MATLAB functions directly from Mathematica and transfer data/variables between the two systems?

share|improve this question
2  
I don't think you can use NETLink on a Mac... –  rm -rf Sep 4 '12 at 22:13
4  
@siva82kb: It might even be possible to use NETLink on a Mac using something like mono. I think I've heard someone do this on linux. On the other hand that won't help as the Mac version of matlab presumably doesn't provide a COM interface, which I think is what you are actually accessing. I think you should first get the information which possibilities there are to interact with matlab from another program on a Mac, presumably the matlab support or community can answer that. With that information, we could probably help you to decide which of those would be easiest to use from Mathematica... –  Albert Retey Sep 5 '12 at 9:20
7  
which Matlab functions do you need to call? And why? Maybe you could achieve the same with Mathematica? I agree that sometimes algorithms are missing in Mathematica (like in NonlinearModelFit) and then you have to use Matlab (lsqcurvefit), but that should be rare. –  Rolf Mertig Sep 5 '12 at 9:58
    
Related SO questions: (1), (2), (3). –  Alexey Popkov Jan 24 '13 at 5:36
2  
Note: This question is being repurposed to be a general repository of information on calling MATLAB from Mma on any platform (including OS X). Please see my answer for a Mac specific solution. –  Szabolcs Jan 26 '13 at 3:16

3 Answers 3

2014-04-12 NOTICE: The recently released MATLAB R2014a contains a bug which partially breaks MATLink (it's not possible to MGet logical type variables, which in turn may have further consequences). If you depend on MATLink, please consider keeping MATLAB R2013b until the problem is sorted out. Due to the nature of the problem there is no quick workaround and a non-trivial fix on the MATLink side would take a while.


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.


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, structs and cells.

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"][]

enter image description here

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.

share|improve this answer
    
That's so cool!! –  user0501 May 2 '13 at 1:15
1  
It actually works! –  bill s May 2 '13 at 4:54
2  
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. –  Daniel W Nov 24 '13 at 13:35

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

"
share|improve this answer

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.

share|improve this answer

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?

Not the answer you're looking for? Browse other questions tagged or ask your own question.