A Mathematica™ application for seamless two-way communication and data transfer with MATLAB™. Harness the computational power of both systems efficiently using MATLink.
Transfer numerical, char, cell and struct arrays of any size and shape from MATLAB as Mathematica lists, strings and rules (and vice versa).
In addition to built-in MATLAB functions, you can also create custom MATLAB functions and scripts on the fly and use them easily from within Mathematica.
Like the data processing capabilities of one system, but the plotting features of the other? No problem! Using MATLink, you can get the best of both worlds.
Substitute missing or subpar capabilities in Mathematica with MATLAB's toolboxes, such as the PDE toolbox or the signal processing toolbox (for Mathematica 8).
Combine the wealth of MATLAB's extensive third-party code base from the File Exchange (and elsewhere) with Mathematica's rich prototyping capabilities.
We know that MATLAB and Mathematica are not cheap, which is why we don't want you to spend a dime more. MATLink is free and will remain free for all purposes.
MATLink has been extensively tested with Mathematica versions 8.0.4, 9.0.1 and 10.0.0, MATLAB 2012b, 2013a and 2013b on OS X 10.8 and 10.9, and moderately tested on Windows 8 and Ubuntu 13.04. If you encounter any issues with a relatively modern operating system or a supported Mathematica/MATLAB version, please open a new issue at GitHub.
Warning: On OS X and Linux, MATLAB R2014a contains a bug that breaks MATLink. Please use an different version of MATLAB with MATLink on these systems. Older or newer versions (R2013b or R2014b) are not affected by the problem. The Windows version of MATLAB R2014a is not affected by this problem and works well with MATLink.
Next: Choose your preferred download option and install MATLink
Download and install MATLink to a location in Mathematica's $Path
. You can do so in one of the following ways:
The zip file will always contain the most up-to-date stable version.
Download the zip file and extract the contents to the folder opened by
SystemOpen@FileNameJoin[{$UserBaseDirectory, "Applications"}]
Next: Select your operating system and follow the instructions to link with MATLAB.
Although MATLink ships with pre-compiled binaries, depending on your specific setup, it might be necessary to manually change a few settings before you can begin to use MATLink. If it does not work out of the box, please try following the instructions below:
bin\win64
directory (bin\win32
for 32-bit versions) to the operating system's PATH
environment variable.regmatlabserver
in MATLAB. On most Windows systems, you will need administrator rights to do this.If you're using a 32 bit MATLAB (such as a student version) with a 64 bit Mathematica, then evaluate the following before using OpenMATLAB[]
for the first time (needs to be done only once)
Needs["MATLink`"] SetOptions[MATLink, "Force32BitEngine" -> True]
If you reset your Mathematica settings or remove MATLink's init.m
in $UserBaseDirectory/ApplicationData/MATLink
, then the above option will need to be set again.
You should now be all set to use MATLink! If for some reason it fails, please open a new issue on GitHub.
Although MATLink ships with pre-compiled binaries, depending on your specific setup, it might be necessary to manually change a few settings before you can begin to use MATLink. If it does not work out of the box, please try following the instructions below:
csh
installed at /bin/csh
(more info).gcc
cannot be used to compile MATLink. The compiler requirements depend on your version of MATLAB and are documented by the MathWorks.libuuid
must be installed.PATH
(i.e., which matlab
and which math
return the correct locations).You should now be all set to use MATLink! If for some reason it fails, please open a new issue on GitHub.
Although MATLink ships with pre-compiled binaries, depending on your specific setup, it might be necessary to manually change a few settings before you can begin to use MATLink. If it does not work out of the box, please try following the instructions below:
MATLink/Engine/bin/MacOSX64
directory, edit the file mengine.sh
and set the path to the MATLAB app bundle. This is not
necessary if MATLAB is installed in the default folder in /Applications
(e.g. /Applications/MATLAB_R2013a.app
)You should now be all set to use MATLink! If for some reason it fails, please open a new issue on GitHub.
To launch MATLink and begin communicating with MATLAB, execute the following in a notebook:
Needs["MATLink`"] OpenMATLAB[]
Throughout the rest of this guide, it is assumed that you're connected to MATLink and the MATLAB workspace is open.
You can execute arbitrary MATLAB code using the MEvaluate
command. Any output that would normally be displayed in MATLAB's command window will be displayed in the output cell. For example, evaluating
MEvaluate["mat = magic(4)"]
will output a magic square of size 3 and make an assignment to a variable named mat
in the MATLAB workspace. Think of MEvaluate
as a portal to the MATLAB workspace, allowing you to run commands in it and assign values to variables.
MEvaluate
, when successful, will always return a string containing the output from MATLAB's command window. It cannot be used to transfer data to Mathematica. Use MGet
and MSet
as explained in the next section.
The functions MSet
and MGet
allow the user to transfer variables to and from the MATLAB workspace. The variable name is always passed as a string. For example, to transfer the data stored in the variable mat
in the MATLAB workspace to Mathematica, evaluate
mat = MGet["mat"];
Now let's transform this variable in Mathematica and send it back to MATLAB:
MSet["mat_tr", Transpose[mat]];
You can verify that a new variable mat_tr
was created in the MATLAB workspace and has the correct value:
MEvaluate[" exist('mat_tr','var') isequal(mat', mat_tr) "]
Since MATLAB is white space insensitive, you can freely use newlines and indentations for clarity in MEvaluate
's argument. Lines not terminated by a semicolon will display the output in Mathematica.
Often one wishes to reuse code found online, either on the MathWorks File Exchange or elsewhere and execute it as a script (i.e., in the base workspace and not as a function). The function MScript
makes it easy to do this. As a simple example, we'll define a script called timing.m
that does some computations using the two variables mat
and mat_tr
defined earlier and displays the timing for 1000 trials:
t = MScript["timing", "tic; for i=1:1000 eig(mat*mat_tr); end toc" ]
You can now run the above script anytime, several times within the current session, by evaluating MEvaluate[t]
(assuming the value for t
has not been cleared) or MEvaluate[MScript["timing"]]
(this will work even if t
is cleared).
Trying to define an MScript
with the same filename twice in the same MATLink session will throw an error. This is to avoid accidental overwrites. You can force an overwrite of the existing file with the option "Overwrite" -> True
for MScript
You can also natively call MATLAB functions already on its path using the MFunction
command. As an example, we'll define and use the magic
function from MATLAB, which is not available in Mathematica:
magic = MFunction["magic"]; magic[4] // MatrixForm
To define a custom function for the current session and use it, use MScript
to save it to a file (remember to use the same filename as the function) and then use MFunction["function_name"]
, where function_name
is the name of your function file.
For some practical real world examples of using MATLink, head over to our examples gallery and also check out the documentation.