Group Abstract Group Abstract

Message Boards Message Boards

Seamlessly call MATLAB from Mathematica

Disclaimer: I am one of the MATLink developers.

Some of you have already seen it, but for those that haven't, I would like to present the MATLink package available at

This package makes it possible to transfer data between Mathematica and MATLAB as well as seamlessly call MATLAB functions from Mathematica.  Some example applications are shown as

As of version 1.0, the package can:
  • Transfer data (variables) between the two systems in both directions.  Most MATLAB data structures are supported, including sparse arrays, cells and structs.
  • Wrap MATLAB functions in the MFunction head and call them directly.  Arguments and return values are translated automatically.
  • It works on all three common platforms (Windows, Mac, Linux).
Note that version 1.0 does not make it possible to call Mathematica functions from MATLAB, only the reverse.  It is useful if Mathematica is your primary work environment but you need to use some MATLAB functionality in your Mathematica code.

Here's a transcript of a short MATLink session as a deomonstration:

 In[1]:= << MATLink`
 In[2]:= OpenMATLAB[] (* start MATLAB *)
 In[3]:= mat = RandomReal[1, {5, 5}]; (* create random matrix *)
 In[4]:= MEvaluate["help eig"] (* get help on the eig() MATLAB function *)
Out[4]= ">> EIG Eigenvalues and eigenvectors.
    E = EIG(X) is a vector containing the eigenvalues of a square 
    matrix X.  

In[5]:= MFunction["eig"][mat] (* calculate eigenvalues using MATLAB *)
Out[5]= {{2.62592 + 0. I}, {-0.0575 + 0.555101 I}, {-0.0575 - 0.555101 I}, {0.558724 + 0. I}, {0.128974 + 0. I}}

In[6]:= Sort@Flatten[%] == Sort@Eigenvalues[mat] (* check that the result is the same that Mathematica gives *)
Out[6]= True
POSTED BY: Szabolcs Horvat
9 months ago
Does it work with Octave?
POSTED BY: Frank Kampas
9 months ago
In its current state MATLink does not work with Octave.

Theoretically it should be possible to adapt MATLink to work with Octave (except for a few features such as unicode support), however, Octave's licensing seems to make this inconvenient at best.  Octave is licensed under the GPL.  This means that we can't link MATLink both against Octave and the (closed source) MathLink libraries and distribute the resulting software.   At least this is what one of the Octave developers told me.

We are not planning to work on Octave support because:
  1. Octave's licensing wouldn't allow distributing compiled MATLink binaries (and at least one Octave developer made it clear that we should comply)
  2. MATLink is primarily useful because it makes it possible to call library/toolbox functions that are available in MATLAB but not Mathematica, and not because it provides access to MATLAB the language.  Octave doesn't have nearly as many tooboxes as MATLAB.  Thus it's less useful to call Octave than to call MATLAB.
  3. While theoretically Octave support is possible, it would require a non-trivial amount of work. Because of point 2. above it doesn't seem to be worth the effort.
POSTED BY: Szabolcs Horvat
9 months ago