# SharedMATLABEngine.jl - Connect the Julia REPL to an open MATLAB session

**URL:** <https://discourse.julialang.org/t/sharedmatlabengine-jl-connect-the-julia-repl-to-an-open-matlab-session/46865>\
**Category:** Package Announcements\
**Created:** [September 18, 2020, 7:15pm UTC](https://discourse.julialang.org/t/sharedmatlabengine-jl-connect-the-julia-repl-to-an-open-matlab-session/46865 "2020-09-18T19:15:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [September 18, 2020, 7:15pm UTC](https://discourse.julialang.org/t/sharedmatlabengine-jl-connect-the-julia-repl-to-an-open-matlab-session/46865/1 "2020-09-18T19:15:57Z")

</div>

[SharedMATLABEngine.jl](https://github.com/jonniedie/SharedMATLABEngine.jl)

[MATLAB.jl](https://github.com/JuliaInterop/MATLAB.jl) is a great tool for interoperability between MATLAB and Julia, but it has one problem: the C MATLAB Engine API it uses doesn’t expose the ability to connect to open MATLAB sessions the way that the Python and C++ APIs do. Since I constantly found myself wanting to transfer data between my Julia and MATLAB sessions, I decided to create SharedMATLABEngine.jl. It uses PyCall to access the Python MATLAB Engine API [matlab.engine](https://www.mathworks.com/help/matlab/matlab-engine-for-python.html).

The intention is not to replace MATLAB.jl, as that is a much more complete project than I’m intending for this to be. It’s really meant just to give you one thing: **an embedded and shared MATLAB command prompt in your Julia REPL**.

Here’s a quick overview of the functionality from the README:

# Overview

To begin using SharedMATLABEngine, import the library and call the function `connect_matlab(engine_name)`. To see a list of available MATLAB sessions, call `find_matlab()`. Alternitavely, you can call `matlab.engine.engineName` in MATLAB to see the name of that specific session.

```julia
julia> using SharedMATLABEngine

julia> eng = connect_matlab("MATLAB_25596"); # Get from matlab.engine.engineName in MATLAB
REPL mode SharedMATLABEngine initialized. Press > to enter and backspace to exit.

```

Now the MATLAB command line can be accessed from the Julia REPL by typing `>`.

```matlab
>> a = magic(3)

a =

     8 1 6
     3 5 7
     4 9 2

```

Julia variables can be interpolated into MATLAB commands via the `$` operator.

```matlab
>> $a21 = a(2,1)
3.0

>> b = {zeros($a21), 'some words'}

b =

  1×2 cell array

    {3×3 double} {'some words'}

>> [$z, c] = b{:}

c =

    'some words'

julia> (a21, z)
(3.0, [0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])

```

MATLAB command outputs can be accessed in Julia through the `mat"` string macro or the `Engine` instance’s workspace field.

```julia
julia> b = a21 .+ mat"a"
3×3 Array{Float64,2}:
 11.0 4.0 9.0
  6.0 8.0 10.0
  7.0 12.0 5.0

julia> sqrt.(eng.workspace.a)
3×3 Array{Float64,2}:
 2.82843 1.0 2.44949
 1.73205 2.23607 2.64575
 2.0 3.0 1.41421

```
