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.

*Mathematica*version 8.0.4 or higher- MATLAB R2011a (version 7.12) or higher
- A version of Windows/Linux/OS X that can run both
*Mathematica*and MATLAB

MATLink has been extensively tested with *Mathematica* versions 8.0.4 and 9.0.1, MATLAB 2012b, 2013a and 2013b on OS X 10.8.2 and 10.8.3, 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 MATLAB R2014a contains a bug that breaks MATLinks. Please use an older version of MATLAB with MATLink until the issue is fixed.

**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"}]

Using git allows you to easily pull changes and keep up-to-date with the current and development versions.

- If you don't have git already installed, you should install it before proceeding.
- Open a terminal window and run the following (change the path separator appropriately for Windows)
- To update to the latest version, run
`git pull`

in your local repository.

git clone git://github.com/rsmenon/MATLink.git $UserBaseDirectory/Applications/MATLink

**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:

- Add MATLAB's
`bin\win64`

directory (`bin\win32`

for 32-bit versions) to the system`PATH`

. - Register the default MATLAB version by running
`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

`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:

- To run the MATLink engine, you must have the C shell
`csh`

installed at`/bin/csh`

(more info). - A C++ compiler is needed to compile the MATLink binary. Very old versions of
`gcc`

cannot be used to compile MATLink. - Both
*Mathematica*and MATLAB must be on the system`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:

- Navigate to the
`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.

- Please help spread the word about MATLink among those that use
*Mathematica*and have use for MATLAB. The more the number of people that use it, the easier it is to find and fix bugs, prioritize new features for future updates, etc. - If you find a bug or unexpected behaviour, please open a new issue at Github and we'll look at it. If you don't have a Github account and don't want to create one, you can also contact us at matlink.m@gmail.com.
- If you have interesting uses for MATLink, please let us know at the above address and we'll eventually add to our examples gallery.