# Calling Julia from MATLAB

**URL:** <https://discourse.julialang.org/t/calling-julia-from-matlab/51542>\
**Category:** General Usage\
**Tags:** embedding, matlab\
**Created:** [December 9, 2020, 6:26pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542 "2020-12-09T18:26:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vangelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vangelis/32/14063_2.png) [@Vangelis](https://discourse.julialang.org/u/Vangelis)\
**Post date:** [December 9, 2020, 6:26pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/1 "2020-12-09T18:26:32Z")

</div>

Hello everyone,  
I am trying to call a Julia algorithm from MATLAB.  
Looking at [https://github.com/juliamatlab/](https://github.com/juliamatlab/) I found the:

- mexjulia
- jlcall

I tried both and, after making some corrections (because are meant for Julia 0.6), I was able to compile the mex file. Following the instructions on the README of both `mexjulia` and `jlcall` I was not able to execute the simple `jl.call('2+2')`.  
Several issues and in most of the times the MATLAB crashes.

Googling around, I found this [[Has anyone had success using Julia from Matlab? - #22 by Chris\_Foster](https://discourse.julialang.org/t/has-anyone-had-success-using-julia-from-matlab/16661/22)] which a bit discouraging.  
Did anyone succeed to call Julia from MATLAB?

Any help is much appreciated!

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [December 9, 2020, 6:45pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/2 "2020-12-09T18:45:03Z")

</div>

I have been using this fork of Mex.jl for a while now:  
[https://github.com/jebej/Mex.jl/](https://github.com/jebej/Mex.jl/)

I have tested it on Windows with MATLAB \> R2018.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [December 9, 2020, 7:34pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/3 "2020-12-09T19:34:00Z")

</div>

> [@Vangelis](#):
>
> Did anyone succeed to call Julia from MATLAB?

I didn’t even try (didn’t need to), but I’ve called in the other direction. And also to Octave (a MATLAB clone).

That’s not a really useful to know information (unless you can do callbacks), but I bring it up as there may be other options.

I didn’t call Octave directly, I call Python, and through Oct2Py indirectly to Octave (it works, one “bug”, and I posted a workaround elsewhere). It’s not as clean, but basically no boilerplate (I guess would be similar in other direction), looks like e.g. this:

```julia
(vintage0, vintage1, vintage2, vintage6m) = py"oc.get_vintage_months($vintageNow, nout=4)"

```

So I was thinking, maybe you can call from MATLAB (or Octave) to Python instead, and then to Julia through pyjulia package. Should work, as I found to Python is a supported MATLAB option:

> **[Call Python Function in MATLAB to Wrap Paragraph Text
- MATLAB & Simulink](https://www.mathworks.com/help/matlab/matlab_external/call-python-from-matlab.html)**
>
> This example shows how to use Python language functions and modules within MATLAB.

And something similar for Octave (I’m not sure how good this is):  
[https://wiki.octave.org/Pythonic](https://wiki.octave.org/Pythonic)

[For me, I could have used Octave in production, if it was up to me, but I was using it as a cruch while porting, and I actually completed a project, fully ported to Julia. What’s you (long-term) goal?]

---

<div class="post-metadata">

**Author:** ![Vangelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vangelis/32/14063_2.png) [@Vangelis](https://discourse.julialang.org/u/Vangelis)\
**Post date:** [December 9, 2020, 7:36pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/4 "2020-12-09T19:36:42Z")

</div>

thanks @jebej for the suggestion!  
I tried to follow the steps on the REAMDE, but I was not able to proceed with the build step.  
What should I run on the MATLAB side to start the build?

thanks!!

---

<div class="post-metadata">

**Author:** ![Vangelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vangelis/32/14063_2.png) [@Vangelis](https://discourse.julialang.org/u/Vangelis)\
**Post date:** [December 9, 2020, 7:39pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/5 "2020-12-09T19:39:49Z")

</div>

Thanks for the answer @Palli!  
Indeed what you suggest would have been easier, but I need to send Julia models to MATLAB users.  
That’s why I am looking into that…

About calling to Python first and then to Julia, it could be an option, although it increases the overhead.  
I hope @jebej’s fork could do the trick 🙂 otherwise I will follow your path!

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [December 9, 2020, 7:44pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/6 "2020-12-09T19:44:56Z")

</div>

Mex.jl also worked for me. I had a similar need:

> [@Passing strings from Matlab to Julia and back using Sockets](https://discourse.julialang.org/t/passing-strings-from-matlab-to-julia-and-back-using-sockets/46589):
>
> I needed a simple communication channel between Matlab and Julia. Maybe this will help someone, so here’s the code (spoiler alert, everything is an ascii character): ## Matlab to Julia TCPIP communication example # Julia - server side using Sockets server = listen(ip"127.0.0.1", 2001) sock = accept(server) x=0 @async while true global x = readline(sock) println(x) end # after receiving a message numeric = parse.(Float64, split(x)) # where x is "-1.2158 1.5908 0.52648", vector cov…

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [December 9, 2020, 7:54pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/7 "2020-12-09T19:54:42Z")

</div>

> [@Vangelis](#):
>
> but I need to send Julia models to MATLAB users.

It’s not clear to me, that changes much. You need to have julia (the runtime) set up, and not a problem, with calling to Python first you need it set up too. For my direction, there’s no great overhead to call Python, at least way more (still not a problem) from there to Octave (Oct2Py, transmits data back and forth my writing to files).

My understanding is pyjulia is as efficient as PyCall.jl (basically implemented by it, should be same overhead, meaning “no copying”).

FYI: See also [GitHub - jonniedie/SharedMATLABEngine.jl: Connect the Julia REPL to a shared MATLAB session.](https://github.com/jonniedie/SharedMATLABEngine.jl)

> SharedMATLABEngine goes through a PyCall and the uses the matlab.engine interface in Python.

I believe it’s only to call in the othe direction, but I could be wrong or there be a workaround (callbacks, since Python used, seems like it similar to pyjulia implemented).

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [December 9, 2020, 8:55pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/8 "2020-12-09T20:55:36Z")

</div>

Are you sure you used my fork? I didn’t change the URL in the readme. If you did, what is the error? Does MATLAB.jl work?

---

<div class="post-metadata">

**Author:** ![Vangelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vangelis/32/14063_2.png) [@Vangelis](https://discourse.julialang.org/u/Vangelis)\
**Post date:** [December 9, 2020, 9:19pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/9 "2020-12-09T21:19:29Z")

</div>

Yes, I used your fork 🙂  
I installed MATLAB.jl on Julia but for the Mex.jl I got an error that it couldn’t find matlab. So, I tried to re-install it and it seemed that it worked, so now both (MATLAB.jl and Mex.jl) are visible in the Julia.  
Moving to the MATLAB environment I was not able to find how to compile the mex file…

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [December 9, 2020, 9:22pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/10 "2020-12-09T21:22:22Z")

</div>

The compilation is actually triggered by julia via MATLAB.jl when you build Mex.jl, so now that you are sure MATLAB.jl works, try `]build Mex`

---

<div class="post-metadata">

**Author:** ![Vangelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vangelis/32/14063_2.png) [@Vangelis](https://discourse.julialang.org/u/Vangelis)\
**Post date:** [December 9, 2020, 9:27pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/11 "2020-12-09T21:27:53Z")

</div>

right!!!  
and the error is:  
`ERROR: LoadError: MATLAB.MEngineError("failed to get variable matlab_jl_has_ans from MATLAB session") `  
Do you think I miss something on the matlab side?

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [December 9, 2020, 9:41pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/12 "2020-12-09T21:41:14Z")

</div>

It looks like MATLAB.jl is not working properly. Make sure you follow [JuliaInterop/MATLAB.jl](https://github.com/JuliaInterop/MATLAB.jl)

You should be able to call `mat"2+2"` and see the result.

---

<div class="post-metadata">

**Author:** ![Vangelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vangelis/32/14063_2.png) [@Vangelis](https://discourse.julialang.org/u/Vangelis)\
**Post date:** [December 9, 2020, 10:37pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/13 "2020-12-09T22:37:36Z")

</div>

Indeed!!!  
that was the problem!! Now it works!!!  
Thank you very much!!!

---

<div class="post-metadata">

**Author:** ![Noodle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noodle/32/22901_2.png) [@Noodle](https://discourse.julialang.org/u/Noodle)\
**Post date:** [April 2, 2021, 12:33pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/14 "2021-04-02T12:33:36Z")

</div>

any thoughts how to proceed?

---

<div class="post-metadata">

**Author:** ![Vangelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vangelis/32/14063_2.png) [@Vangelis](https://discourse.julialang.org/u/Vangelis)\
**Post date:** [April 2, 2021, 3:29pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/15 "2021-04-02T15:29:53Z")

</div>

How do you mean?

---

<div class="post-metadata">

**Author:** ![Noodle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noodle/32/22901_2.png) [@Noodle](https://discourse.julialang.org/u/Noodle)\
**Post date:** [April 3, 2021, 8:11am UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/16 "2021-04-03T08:11:40Z")

</div>

I followed the instruction and have installed Matlab.jl from Julia.

Now when I try jl.eval(‘2+2’) in matlab, it shows

It appears the mexjulia MEX function is missing. Consider building “Mex.jl”.\n

And I couldn’t even do jl.config in Matlab, it says

jl has no static method as ‘config’, any thoughts? Thanks

---

<div class="post-metadata">

**Author:** ![Noodle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noodle/32/22901_2.png) [@Noodle](https://discourse.julialang.org/u/Noodle)\
**Post date:** [April 3, 2021, 8:38am UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/17 "2021-04-03T08:38:40Z")

</div>

When I do jl.config in matlab, it shows

_The class jl has no Constant property or Static method named ‘config’._

Did you run jl.config in Matlab before you call other jl functions? Which directory did you run that command on?

---

<div class="post-metadata">

**Author:** ![Noodle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noodle/32/22901_2.png) [@Noodle](https://discourse.julialang.org/u/Noodle)\
**Post date:** [April 3, 2021, 12:10pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/18 "2021-04-03T12:10:47Z")

</div>

Now in Julia I can call mat"2+2" and see the result 4.0. And in Julia I can do

using MATLAB  
using Mex  
build Mex

so I guess the MATLAB.jl and Mex.jl are properly installed in Julia?

The problem is when I come back to Matlab and call jl.config it has the following error

The class jl has no Constant property or Static method named ‘config’.

Do you think it might be a version problem?

I have tried MATLAB R2018b and R2020b with Julia 1.5.3.

Many thanks!

---

<div class="post-metadata">

**Author:** ![Vangelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vangelis/32/14063_2.png) [@Vangelis](https://discourse.julialang.org/u/Vangelis)\
**Post date:** [April 3, 2021, 7:31pm UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/19 "2021-04-03T19:31:11Z")

</div>

You no longer need the “config” .  
The `jl.config` is for the other way of compiling the mex in Matlab.  
But you already did it, in Julia.  
That is why you can run the “2+2” .

---

<div class="post-metadata">

**Author:** ![Noodle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noodle/32/22901_2.png) [@Noodle](https://discourse.julialang.org/u/Noodle)\
**Post date:** [April 4, 2021, 1:30am UTC](https://discourse.julialang.org/t/calling-julia-from-matlab/51542/20 "2021-04-04T01:30:28Z")

</div>

Thanks! But I can only do mat"2+2" in julia, but not jl.eval(‘2+2’) in Matlab.

When I do jl.eval(‘2+2’) in matlab, the error is

_It appears the mexjulia MEX function is missing. Consider building “Mex.jl”.\n_

Do you know what the issue is? thank you!
