Group Abstract Group Abstract

Message Boards Message Boards

14
|
33.1K Views
|
21 Replies
|
33 Total Likes
View groups...
Share
Share this post:

Seamlessly call MATLAB from Mathematica

Posted 13 years ago
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 matlink.org:



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 http://matlink.org/examples/

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 Horvát
21 Replies

Kudos to all developers! This is very useful for folks who have access to both systems.

POSTED BY: Matthias Odisio
Posted 12 years ago

I personally would find the capability of single line input in a command window quite useful (although command recall would be highly desirable), but can't speak for others as my use of Matlab and Mathematica may not conform to the way most people use these programs. I sometimes use Matlab almost like a highly capable calculator where I enter commands on the command line and use the results of a command to decide the next command to enter. I often use the Mathematica the same way until I have decided how to proceed to solve a problem. This way of operating, of course, is possible without the command window but each command would need to be quoted and wrapped with an MEvaluate which requires extra typing.

POSTED BY: Mike Luntz

Well, it all comes down to having to implement the command window in pure MATLAB and preferably without using undocumented features.

If anyone has ideas or suggestions on how to do this and come up with a useful and usable command window, please email me.

POSTED BY: Szabolcs Horvát
Posted 12 years ago
POSTED BY: Mike Luntz

Good to hear that you found the command window useful. I wasn't sure anyone was using it at all, as it's much more limited than the standard MATLAB command window which has syntax colouring, auto-completion, command recall, etc.

Does that mean that you'd find a command window without these features (without command recall, auto-completion, or even multi-line input) useful too? It may be possible to implement something in terms of input. Try e.g. waitInput=1;while waitInput; cmd=input('> ', 's'); eval(cmd); end in MATLAB. I assumed that something like this is so limited that it is simply not useful.

POSTED BY: Szabolcs Horvát
Posted 12 years ago
POSTED BY: Mike Luntz
POSTED BY: Szabolcs Horvát
Posted 12 years ago

Thanks for the help. Based on Ravi's recommendation I gave MATLink a try. I am happy to report that, at least for the simple first couple of commands in the "Getting Started" section of the web site, MATLink appears to work with Matlab2008b. Should I encounter any compatibility problems between MATLink and this old version of Matlab would you like for me to report it and, if so, should it be reported in this conversation?

One additional question: Do I need the

SetOptions[MATLink, "Force32BitEngine" -> True]

command each time I load MATLink or, as Ravi seemed to indicate, only once?

POSTED BY: Mike Luntz
POSTED BY: Szabolcs Horvát
Posted 12 years ago

Thanks for the quick response to my question. I am currently using Matlab R2008b and Mathematica 10.0.1 on a Windows 7 computer. But I am considering a switch to a Mac and when I do that I will transfer the Mathematica license and upgrade Matlab.

POSTED BY: Mike Luntz
Posted 12 years ago
POSTED BY: R Menon
Posted 12 years ago

I have a list in Mathematica of the form

lis= {{1,"abcd"},{2,"ef"},{3,"xyzwabc"}}

and I am wondering if MATLink would be capable of transferring this list to Matlab as a cell array and, if so, how would I do the transfer. I unfortunately do not currently have a new enough version of Matlab to use MATLink or I would try it to see if it worked.

POSTED BY: Mike Luntz
POSTED BY: Szabolcs Horvát

I unfortunately do not currently have a new enough version of Matlab to use MATLink or I would try it to see if it worked.

What version of MATLAB do you have and what operating system are you using?

POSTED BY: Szabolcs Horvát

Can I use CVX in MATLAB through MATLink?

http://cvxr.com/cvx/

POSTED BY: Frank Kampas
POSTED BY: Szabolcs Horvát
Posted 12 years ago
POSTED BY: R Menon

Thanks

POSTED BY: Frank Kampas

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 Horvát
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 Horvát
Does it work with Octave?
POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard