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
11 months ago
Does it work with Octave?
POSTED BY: Frank Kampas
11 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
11 months ago

MATLink 1.1 is now released.

This version brings full compatibility with Mathematica 10. If you use Mathematica 10, please upgrade.

Also, a warning: on Linux & OS X MATLAB R2014a has a bug which prevents MATLink from working correctly. Please do not use this version of MATLAB with MATLink on Linux or OS X. R2013b works fine, and so will R2014b when it is released (probably this September).

Windows users can use R2014a without problems.

POSTED BY: Szabolcs Horvat
1 month ago

Can I use CVX in MATLAB through MATLink?

POSTED BY: Frank Kampas
1 month ago

I am not familiar with CVX, so I do not know.

Things that might prevent you from using it:

  1. Does it rely on callbacks to Mathematica? E.g. would you need to pass a function implemented in Mathematica to it? That is not supported at the moment.

  2. Would it require transferring MATLAB objects (i.e. classes) between MATLAB and Mathematica? This isn't supported either, but structs do work (you can convert the class to a struct first). All major datatypes are supported except classes, function handles and certain rarely used integer types. (We can add support for these integer types on request).

  3. Does it require a very large number of calls to MATLAB? There is an overhead to each call so this might be slow.

If none of these apply then you should be able to use MATLink with CVX.

Point 1. will eventually be dealt with in MATLink 2.x (not 2.0). Point 3: the overhead will be significantly reduced in MATLink 2.0. Point 2 might be partially dealt with in MATLink 2.0, but full support for bi-direction transfer of arbitrary classes is unlikely.

Now if only I had time to work on version 2 ... My job doesn't leave much time for it these days.

POSTED BY: Szabolcs Horvat
1 month ago

You can definitely use MATLink with CVX or any other toolbox for that matter! The only constraint is that the quantities you transfer back to Mathematica should be ones that are [supported by MATLink]( (As a fun fact, my personal motivation for developing MATLink with Szabolcs was so that I can use Mathematica + CVX in my research.)

The following is an example of [fitting an optimal $L_∞$ norm polynomial]( using CVX and Mathematica via MATLink. First, we setup the problem in CVX and use MEvaluate to run it on the MATLAB side (note: You will need to know how to use CVX).

n = 6;
m = 40;

% generate 50 ponts ui, vi
u = linspace(-1,1,m);
v = 1./(5+40*u.^2) + 0.1*u.^3 + 0.01*randn(1,m);

A = vander(u');
A = A(:,m-n+[1:n]);

% L-infty fit
cvx_begin quiet
variable x_inf(n)
minimize (norm(A*x_inf - v', inf))

CVX is quite verbose and it is a bit more efficient to use quiet when running it via MATLink. There are other CVX global variables (starting with cvx_) that you can use to programmatically check which solver was used, whether the problem was solved or if it is infeasible, etc. rather than relying on the textual output. If you are doing active algorithm development using CVX, then it might be better to use MATLAB directly for this part and then use MATLink once you are satisfied and have frozen the routine.

Next, we transfer the optimal solution and the original quantities to Mathematica using MGet:

{u, v, A, xinf} = MGet[{"u", "v", "A", "x_inf"}];

Finally, we compute the optimal $L_2$ norm solution in Mathematica, construct the polynomial using Horner's method and plot them in Mathematica:

With[{hornerPoly = Function[{var, x}, Fold[# var + #2 &, x]]},
ListPlot[Transpose@{u, v}, PlotStyle -> {AbsolutePointSize[5], Gray}],
Plot[{hornerPoly[x, x2], hornerPoly[x, xinf]}, {x, -1, 1}, Evaluated -> True,
PlotLegends -> {"!(*SubscriptBox[(L), (2)])", "!(*SubscriptBox[(L), ([Infinity])])"}]


Hope that helped!

POSTED BY: Ravi Menon
1 month ago


POSTED BY: Frank Kampas
1 month ago