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


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.

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:


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> 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 >.

>> a = magic(3)

a =

     8     1     6
     3     5     7
     4     9     2

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

>> $a21 = a(2,1)

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