# Rewriting a MATLAB library in Julia

**URL:** <https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515>\
**Category:** General Usage\
**Tags:** matlab\
**Created:** [January 31, 2024, 1:47pm UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515 "2024-01-31T13:47:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackmarry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackmarry/32/47139_2.png) [@jackmarry](https://discourse.julialang.org/u/jackmarry)\
**Post date:** [January 31, 2024, 1:47pm UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/1 "2024-01-31T13:47:30Z")

</div>

I am rewriting a MATLAB library in Julia, but I’ve noticed significant differences between Julia syntax and MATLAB. Additionally, GPT often provides incorrect responses. Is there a website that collects tables mapping MATLAB functions to their equivalent functions in Julia?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 31, 2024, 1:49pm UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/2 "2024-01-31T13:49:05Z")

</div>

there’s [Noteworthy Differences from other Languages · The Julia Language](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-MATLAB)

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [January 31, 2024, 2:59pm UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/3 "2024-01-31T14:59:50Z")

</div>

Also: [Native Julia noteworthy differences from MATLAB · MatLang (juliahub.com)](https://docs.juliahub.com/General/MatLang/stable/juliavsmatlab/)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 31, 2024, 3:32pm UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/4 "2024-01-31T15:32:46Z")

</div>

when searching the repl help for functions, you might find the function `apropos` useful.  
Example:

```julia
julia> apropos("least squares")
Base.:\
LinearAlgebra.qr
LinearAlgebra.pinv
LinearAlgebra.LAPACK.gels!
LinearAlgebra.qr
Base.:\
...

```

You can also call it by enclosing your help query with `""`, i.e.

```julia-repl
help?> "least squares"
Base.:\
LinearAlgebra.qr
LinearAlgebra.pinv
LinearAlgebra.LAPACK.gels!
LinearAlgebra.qr
Base.:\

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 31, 2024, 4:21pm UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/5 "2024-01-31T16:21:45Z")

</div>

> [@jackmarry](#):
>
> mapping MATLAB functions to their equivalent functions in Julia?

Bear in mind you sometimes will have to change default arguments for the same results, sometimes there isn’t a satisfactory equivalent because the implementation differs even though the results don’t, and sometimes there isn’t an equivalent at all. Less of a problem but just as annoying is that one-to-one mapping can be unidiomatic, for example ~~MATLAB’s `max([x, y])` would be translated call-wise to Julia’s `maximum([x, y])`, but `max(x, y)` would be preferred to avoid the vector allocation.~~(possibly applies to 3+ scalars comparison `max([x, y, z])` instead).

---

<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:** [January 31, 2024, 4:36pm UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/6 "2024-01-31T16:36:33Z")

</div>

You don’t have to translate/map everything (just use, or read its mapping if you want to map):

> **[MatlabCompat.jl - MATLAB/Octave functions library for Julia](https://matlabcompat.github.io)**
>
> MatlabCompat.jl - MATLAB/Octave functions library for Julia

> **[Progressing from MATLAB to Julia: A Guide to Getting Started](https://www.datacamp.com/blog/progressing-from-matlab-to-julia)**
>
> Check out our guide for MATLAB programmers interested in transitioning to the Julia programming language. Find out everything you need to know to get started with Julia.

You can also just call to (or from) MATLAB:

> **[GitHub - JuliaInterop/MATLAB.jl: Calling MATLAB in Julia through MATLAB Engine](https://github.com/JuliaInterop/MATLAB.jl)**
>
> Calling MATLAB in Julia through MATLAB Engine. Contribute to JuliaInterop/MATLAB.jl development by creating an account on GitHub.

> [@\[ANN\] JuliaFromMATLAB.jl - Call Julia from MATLAB](https://discourse.julialang.org/t/ann-juliafrommatlab-jl-call-julia-from-matlab/66882):
>
> Inspired by this [recent discussion](https://discourse.julialang.org/t/calling-julia-from-matlab-the-smoothest-path-to-sneak-julia-into-a-company/64845) regarding the state of calling Julia from MATLAB, I have put together a package to try to resolve some of the issues faced by previous approaches: [JuliaFromMATLAB.jl](https://github.com/jondeuce/JuliaFromMATLAB.jl). Please scroll down to see the Quickstart guide, pasted here from the package’s README for convenience. In particular, I have chosen to avoid trying to keep up with the MATLAB Mex C/C++ interface. Instead, communication between MATLAB and Julia is performed by writing to temporary .mat files. This…

You CAN also call to Octave, the MATLAB clone language, then you have standard functions, lose out on toolboxes. But I couldn’t use the library for that at the time, and it’s still unchanged for 4 years now.

But I could call Python (then with PyCall, now would use PythonCall), and to Octave from it… Calliing Python in-process, not to Octave from it, it still works. And I had to disable OpenBLAS threading with an env var, do get around a bug.

This might help you migrate (I did in stages), or actually not migrate, just use MATLAB code.

There IS a MATLAB code translator available online, but it doesn’t handle all syntax, nor semantic differences:

> **[MATLAB to Julia translator | MATLAB to Julia converter](https://lakras.github.io/matlab-to-julia/)**
>
> Convenient online tool automatically translates MATLAB source code into Julia, so you can move on to the more interesting task of parallelizing your code.

---

<div class="post-metadata">

**Author:** ![jackmarry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackmarry/32/47139_2.png) [@jackmarry](https://discourse.julialang.org/u/jackmarry)\
**Post date:** [January 31, 2024, 4:47pm UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/8 "2024-01-31T16:47:52Z")

</div>

I have found that this converter frequently encounters errors. [MATLAB to Julia translator | MATLAB to Julia converter](https://lakras.github.io/matlab-to-julia/)

---

<div class="post-metadata">

**Author:** ![jClugstor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jclugstor/32/32689_2.png) [@jClugstor](https://discourse.julialang.org/u/jClugstor)\
**Post date:** [January 31, 2024, 6:20pm UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/9 "2024-01-31T18:20:29Z")

</div>

I’ve used that before, it’s pretty good for many of the obvious syntax things. It does say it’s not completely comprehensive.

> This translator is not comprehensive, but it should accurately translate enough of the most common statements that most of the tedious work of translating the code by hand is eliminated.

But there won’t be one magic bullet that will translate everything in MATLAB to Julia for you (except in very simple cases), since function names, packages, and functionality are different between the two languages.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 31, 2024, 6:52pm UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/10 "2024-01-31T18:52:53Z")

</div>

> [@Benny](#):
>
> MATLAB’s `max([x, y])` would be translated call-wise to Julia’s `maximum([x, y])`, but `max(x, y)` would be preferred

Actually, in Matlab one would write `max(a, b)` like in Julia, so no translation is necessary.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 31, 2024, 7:11pm UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/11 "2024-01-31T19:11:31Z")

</div>

Seems that way, it either changed since the last time I had to seriously use MATLAB or I mixed it up with the 3+ scalar input case `max([a, b, c])`.

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [February 5, 2024, 6:43am UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/12 "2024-02-05T06:43:38Z")

</div>

I have done this with a fairly large collection of solvers. The only way I could do a decent job  
getting from  
[this Matlab](https://archive.siam.org/books/kelley/fr16/) and  
[this other Matlab](https://archive.siam.org/books/fa01/)  
to  
[this Julia](https://github.com/ctkelley/SIAMFANLEquations.jl)  
was to essentially start from scratch. I rapidly found that the Matlab-\>Julia mapping was  
less reliable, no fun, and buggy. The algorithm-\>Julia mapping worked a lot better and I never  
had to look at my old Matlab codes at all.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 5, 2024, 9:34am UTC](https://discourse.julialang.org/t/rewriting-a-matlab-library-in-julia/109515/13 "2024-02-05T09:34:19Z")

</div>

I think many coders have also now experienced that direct translation from Matlab to Julia frequently results in poor performance, and un-idiomatic code.

When programming in ‘Matlab style’, Julia has few benefits to offer, since Matlab is highly optimized for vectorized function calls, with lots of temporary arrays being created and discarded. Julia, too, can vectorize calls efficiently, but is less optimized for frequent array creation/destruction.

@jackmarry If the Matlab code is quite serial and ‘loopy’, you may get a nice benefit from a fairly straightforward translation, but if it is highly optimized to Matlab’s peculiar strengths, you may be better served by doing some re-design. Quite possibly this may lead to cleaner and clearer code as well.
