# Embedding Julia in Matlab

**URL:** https://discourse.julialang.org/t/embedding-julia-in-matlab/4475
**Category:** General Usage
**Tags:** embedding
**Created:** [June 26, 2017, 7:06pm UTC](https://discourse.julialang.org/t/embedding-julia-in-matlab/4475 "2017-06-26T19:06:20Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 26, 2017, 7:06pm UTC](https://discourse.julialang.org/t/embedding-julia-in-matlab/4475/1 "2017-06-26T19:06:20Z")

</div>

In need to port code from Matlab to Julia, and as intermediate step  
it would e very useful to be able to call Julia functions from Matlab.  
The project [https://github.com/twadleigh/mexjulia](https://github.com/twadleigh/mexjulia) has the aim to  
make this possible, but it doesn’t work on Linux (and not sure if  
it works with Julia 0.6 on any other platform).

Therefore I need help to debug and fix the following issue:  
[https://github.com/twadleigh/mexjulia/issues/32](https://github.com/twadleigh/mexjulia/issues/32)

Is here someone around who could help to understand and fix  
this issue?

Any help appreciated!

Uwe

---

<div class="post-metadata">

### Author: ![shorty66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shorty66/32/3254_2.png) [@shorty66](https://discourse.julialang.org/u/shorty66)
#### Post date: [June 26, 2017, 8:27pm UTC](https://discourse.julialang.org/t/embedding-julia-in-matlab/4475/2 "2017-06-26T20:27:35Z")

</div>

It works quite well with 0.6 on windows. I use it one a regular basis.  
I use MATLAB r2015b and Windows 10.

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)
#### Post date: [June 26, 2017, 10:48pm UTC](https://discourse.julialang.org/t/embedding-julia-in-matlab/4475/3 "2017-06-26T22:48:40Z")

</div>

Apparently [other people](https://github.com/twadleigh/mexjulia/issues/39) have problems on Windows (which I believe is the twadleigh’s primary platform). Given the unclear nature of that linux issue, and a number of conversations about memory handling and interrupts in the past when I have looked at this, I suspect there are problems here which are too deep to debug by proxy. I just looked through some of the code and took a few notes:

- Very hard to understand the flow of control and design decisions [here](https://github.com/twadleigh/mexjulia/blob/66b50cd2c342fdfb60eb57f56056a67fd049671d/jl/Mex.jl#L107-L184).
- A number of suspicious (or at least odd) uses of `unsafe_wrap` in the array handling Julia code.
- Only two of the rooted arguments [here](https://github.com/twadleigh/mexjulia/blob/66b50cd2c342fdfb60eb57f56056a67fd049671d/mexjulia.cpp#L43-L50) are used (but `jl_call2` roots all of its arguments anyway) – maybe harmless, but a code smell.

Given the state of the code, if I was working on this, I would start from close to scratch (possibly using `loadlibrary` rather than mex) and do some minimal experiments first: basic code eval, wrapping arrays and type conversion for a small number of types, etc. That’s the only way I would feel sufficiently informed to judge whether and how to clean-up versus rebuild.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 27, 2017, 12:09am UTC](https://discourse.julialang.org/t/embedding-julia-in-matlab/4475/4 "2017-06-27T00:09:34Z")

</div>

The loadlibrary approach sound promising for me…  
How do I convert a Julia function into a shared library (.so file)?

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)
#### Post date: [June 27, 2017, 1:36am UTC](https://discourse.julialang.org/t/embedding-julia-in-matlab/4475/5 "2017-06-27T01:36:02Z")

</div>

I’m not sure how that would help with the interactive use case of mexjulia. For that I’m referring to calling julia embedding API via loadlibrary, which I’ve never used, but is apparently something more like an ffi (compared to mex).

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 27, 2017, 6:47pm UTC](https://discourse.julialang.org/t/embedding-julia-in-matlab/4475/6 "2017-06-27T18:47:49Z")

</div>

Well, while interactive use is nice, this is not my main requirement. I just want to be able to implement computational expensive flight control models in matlab/simulink, which then could be used to develop the controller. Ok, in the documentation it is explained how to do that for embedding Julia in a C program, but not how to embed it in a Matlab program.  
Is there a tutorial out there how to do this, using loadlibrary on the Matlab side?

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)
#### Post date: [June 28, 2017, 4:10am UTC](https://discourse.julialang.org/t/embedding-julia-in-matlab/4475/7 "2017-06-28T04:10:01Z")

</div>

~~I read [this post](http://bastibe.de/2016-05-31-Matlab-FFI.html) – I would guess there is official documentation too. The idea is similar to python ctypes, Julia `ccall`, LuaJIT FFI, etc.: load a shared library (in this case `libjulia.so`) at runtime and call some functions, without having directly linked anything at (static) compile-time.~~

[edit: I misunderstood, and this interpretation was incorrect. It still requires a local C compiler, so not really any more helpful than mex here]

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 29, 2017, 9:11pm UTC](https://discourse.julialang.org/t/embedding-julia-in-matlab/4475/8 "2017-06-29T21:11:40Z")

</div>

Thanks for the hint!  
On Linux, you always have a local C compiler available, so this is not an issue. It looks that this approach still might be easier than mex.
