# Using the variable in the MATLAB workspace using MATLink or any other packages

I have a program in MATLAB and after I run the program, I get some results in MATLAB's workspace. Now, I need to use these results in Mathematica. What I currently do is that I save the workspace as a .mat file and import it in Mathematica. This process can be time consuming.

I installed the MATLink package and now I'm able to use MATLAB commands in Mathematica. The problem is, I don't have access to the workspace of my current MATLAB window and it seems as if MATLink created a workspace of its own.

Is there a way to access the variables in my current MATLAB workspace from Mathematica?

• Are you on Windows? – Szabolcs Dec 31 '14 at 17:23

MATLink 1.x is based on the MATLAB Engine interface, which sets its limitations and possibilities. What you are asking for is possible on Windows, but not on Mac or Linux.

By default MATLink will start a new instance of MATLAB in the background. On Windows and Mac you can see this instance appear in the taskbar or dock. All MATLAB commands are evaluated using this instance, thus the workspace is not shared with any other MATLAB you might have started separately.

On Windows the MATLAB Engine is based on the MATLAB COM Automation Server. It is possible to use the currently running instance of MATLAB as Automation Server. Here's the recipe:

1. Start MATLAB first (before starting MATLink)
2. In MATLAB, evaluate enableservice('AutomationServer', true)
3. Now start MATLink using Needs["MATLink"]; OpenMATLAB[]
4. Test that the workspace is shared by creating a variable using MATLink: MEvaluate["asd = 12321"]` and checking that it exists in the workspace of the open MATLAB window.

On macOS and Linux this is not possible due to the limitations of the public MATLAB Engine C API. I have also confirmed this with The MathWorks support.

While it is possible to take control of an already running MATLAB on Mac/Linux too, as evidenced by the Python and Java MATLAB Engine APIs, the underlying C++ API that makes this possible is not made public. Therefore MATLink cannot use it.

• This will save me a lot of time! It works exactly the way I wanted. Thanks a million. – secluded Jan 1 '15 at 10:40