MATLAB-to-Julia Translator Krasilnikova
MATLAB-to-Julia Translator Krasilnikova
Lydia A. Krasilnikova
December 23, 2013
Abstract
Some of the fields that could most benefit from parallelization primar-
ily use programming languages that were not designed with parallel com-
puting in mind. The MATLAB-to-Julia translator proposed here begins
to approach this problem starting with MATLAB, which is syntactically
close to Julia. The translator does much of the tedious work of converting
source code from MATLAB to Julia, in hopes that a MATLAB user who
is curious about Julia could then spend most of their first moments with
the language exploring its capacity to improve their existing programs
rather than wrangling with bugs or a new syntax. Hopefully with time
and input from other Julia users this translator will become a powerful
tool and perhaps lower the barrier to switching to Julia.
1 Background
Many fields, such as mechanical engineering, linear algebra, and computational
biology, have massive potential for parallelization and a good fit with Julia’s
strengths. Unfortunately the existing code in these fields is already in other
languages, and this on its own limits users’ ability to try using Julia. There
is first the problem of needing to learn and code in a new language, which is
often intimidating. In addition the existing code, which could be chopped up
and reused, cannot be copy-pasted into Julia like it could the original language.
There are currently no tools for translating source code into Julia from an-
other language. The benefit of such tools could be vast: users could have more
flexibility in their programming language, more users could be exposed to Julia,
and fields for which Julia might be a good fit might be more likely to try it on.
2 Project Goals
The goal of this project is to build an easy-to-use tool for translating MATLAB
source code into Julia. The translator does not need to be comprehensive, but
it does need to be able to accurately translate enough of the most common
statements that most of the tedious work of translating the code by hand is
eliminated. The hope is that the user can then review the translated Julia code
and perhaps make minor corrections, but be able to quickly move on to the
more interesting task of parallelizing their code.
Because the focus of this project is to minimize barriers for potential Julia
users, it is important that it be easy and pleasant to use. Therefore another
1
goal of the project is to make the translator as accessible as possible, minimizing
load time and barriers to access. In addition, it is important that the translated
code be easy to read and that is feels like a di↵erent version of the user’s own
original code. It is important that the translation not introduce new bugs, if
possible, and that any new bugs that are introduced be easily identified and
resolved. It is also important that the personal style of the author who wrote
the original code is preserved in the translation.
3 Implementation
The translator consists of two parts: the first is a front end user interface,
written in Java; the second is the back end translator, written in Perl.
The Java user interface allows users to type in MATLAB source code or load
it from a file and then manipulate it. When the MATLAB code is ready the
user can press a button to translate the source code into Julia.
When the user initiates translation, the MATLAB source code is saved to
a temporary file in the same directory as our program and control is handed
over to the back end, the Perl translator script. The Perl translator script reads
the temporary MATLAB file, translates it into Julia, and saves the translation
to a second temporary file. Control is finally returned to the user interface,
which reads in and displays the translated Julia code and deletes both of the
temporary files.
The user is then able to manipulate and save the resulting Julia code to a
file, or continue editing and translating the MATLAB source code.
2
Figure 1: A screenshot of the translator interface.
The user can continue editing both the MATLAB and the Julia code. When-
ever a new translation is performed, the translated text replaces the previous
contents of the Julia text area. Similarly, when new MATLAB code is loaded
from a file, it replaces the previous MATLAB code and any changes the user
may have made.
The front end Java interface waits for the Perl script to complete its job from
a background thread. This allows the interface to remain responsive even while
the Perl script is running. However, any changes the user might make would not
be included in the current translation; therefore the fields and buttons of the
interface are disabled until the Perl script is done running, and the background
color of the interface changes to a dark grey. The interface is similarly disabled
when there are error messages to the user.
3
and back end are separate, the back end can be edited and tested without
restarting the front end or reloading the test code.
%{ #
Block Comments comment # comment
%} #
Semicolons a = 1 + 2; b = 4; a = 1 + 2; b = 4
Commas a = 1 + 2, b = 4, a = 1 + 2; b = 4
‘hello’ “hello”
Quotes ‘hi’ “hi”
‘h’ ‘h’
using PyPlot
x = 0:0.05:5; x=0:0.05:5
2D Plotting y = sin(x.ˆ2); y=sin(x.ˆ2)
(example from plot(x, y); plot(x, y)
MathWorks.com) xlabel(‘Time’) xlabel(“Time”)
ylabel(‘Amplitude’) ylabel(“Amplitude”)
4
MATLAB code Julia translation
5
4 Future Directions
I have posted the source code on GitHub. My hope is that other Julia users will
find this project as interesting as I have and help to make it a truly powerful
tool.
A good place to start improving the translator might be the list of notewor-
thy di↵erences from MATLAB in the Julia docs. Some particularly useful areas
for improvement include broadening the supported plotting functions, develop-
ing a more intelligent di↵erentiation between matrix indexing and function calls,
and incorporating the small di↵erences between how the two languages process
matrices.
It might also be interesting to compile a list of common statements and tools
in MATLAB that do not translate to Julia and which other people might find
interesting and implement.
6
usually take to load might be prohibitive. The wait time could become another
barrier.
An alternative is to translate the Java interface into JavaScript. I have
very little experience with JavaScript or with running Perl scripts online, but I
hope that with generous help and time it could meld more seamlessly with the
browser window and take fewer steps to run.
Once the front end has been revised and put online, it would also be helpful
to the user if the program were to suggest code in the Julia translation that
could be parallelized and link to appropriate Julia docs explaining how to do
it. In addition, it would be useful if the program could highlight areas of code
that either could not be translated at all or that could not be translated with
confidence.
7
• The Julia docs have very detailed explanations in addition to code ex-
amples. I was able to get a good grasp of bigger pictures in addition to
smaller details when I needed them.