Calling Julia from MATLAB

Hello everyone,
I am trying to call a Julia algorithm from MATLAB.
Looking at 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'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?] which a bit discouraging.
Did anyone succeed to call Julia from MATLAB?

Any help is much appreciated!

Thanks in advance!

I have been using this fork of Mex.jl for a while now:

I have tested it on Windows with MATLAB > R2018.

1 Like

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:

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

And something similar for Octave (I’m not sure how good this is):

[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?]

1 Like

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 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 :slight_smile: otherwise I will follow your path!

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


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

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

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?

Yes, I used your fork :slightly_smiling_face:
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…

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

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?

It looks like MATLAB.jl is not working properly. Make sure you follow JuliaInterop/MATLAB.jl

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

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

any thoughts how to proceed?

How do you mean?

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

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?

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!

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

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!