# \[ANN\] JuliaFromMATLAB.jl - Call Julia from MATLAB

**URL:** <https://discourse.julialang.org/t/ann-juliafrommatlab-jl-call-julia-from-matlab/66882>\
**Category:** Package Announcements\
**Tags:** interoperability, matlab\
**Created:** [August 23, 2021, 11:57pm UTC](https://discourse.julialang.org/t/ann-juliafrommatlab-jl-call-julia-from-matlab/66882 "2021-08-23T23:57:04Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [August 24, 2021, 5:59pm UTC](https://discourse.julialang.org/t/ann-juliafrommatlab-jl-call-julia-from-matlab/66882/5 "2021-08-24T17:59:17Z")

</div>

Thanks @jondeuce !

Yes, I was looking for some time [into this](https://discourse.julialang.org/t/calling-julia-from-matlab-the-smoothest-path-to-sneak-julia-into-a-company/64845) and came to about the same conclusions: command-line (non-API) and MAT.jl. My code uses [Java-streams](https://stackoverflow.com/questions/68822890/a-java-matlab-julia-interface) and a simple julia -iq call, not requiring DaemonMode.jl.

This is one of my very first Julia codes, so no bells and whistles as your implementation.  
Also, my call pattern is a bit different,

```julia
 jl.call('[a, m] = addmult', 2, 3);
 a+m
 ans =
     11

```

I am curious to see how your matlabify type translation and other features chime in.

> **jlcall.jl**
>
> ```julia
> #=
> jlcall.jl
> Matlab-Julia interface, called from jlcall.m
> =#
> 
> using MAT
> 
> function jlcall(iofile)
> # read call arguments from iofile
> matvars = matread(iofile)
> outvars = matvars["outvars"]
> fun = matvars["fun"]
> sfun = Symbol(fun)
> inputs = matvars["varargin"]    
> # if necessary, load function, run Julia
> if true #!(@isdefined sfun)
> funfile = matvars["fun"] * ".jl"
> println("...loading $funfile")
> include(funfile)
> end
> results = @eval $sfun($inputs...) 
> # save output arguments to iofile
> d = Dict{String, Any}()
> for i = 1:length(outvars)
> d[outvars[i]] = results[i]
> end
> matwrite(iofile, d)
> println("OK")
> end
> 
> ```

> **jlcall.m**
>
> ```julia
> function jlcall(outvars_fun, varargin)
> %JLCALL Execute Julia function from Matlab
> % jlcall('[outvar1, outvar2, ...] = fun', var1, var2, ...)
> % fun is a Julia function, built-in or defined in <fun>.jl
> %
> % Example:
> %
> % >> jlcall('[a, m] = addmult', 2, 3)
> % >> a+m
> % ans =
> % 11
> 
> outvars_fun = split(outvars_fun, {'[',']','=',',',' '});
> outvars_fun(cellfun('isempty',outvars_fun)) = [];
> outvars = outvars_fun(1:end-1);
> fun = outvars_fun{end};
> 
> iofile = [tempname, '.mat'];
> save(iofile, 'outvars', 'fun', 'varargin');
> system(sprintf('julia jlcall.jl %s', strrep(iofile, '\', '/')));
> load(iofile, outvars{:});
> system(sprintf('rm %s', iofile));
> end
> 
> ```

---

_[View the full topic](https://discourse.julialang.org/t/ann-juliafrommatlab-jl-call-julia-from-matlab/66882)._
