A python script to convert matlab files or lines of matlab code to python. This project is in alpha phase. This implementation relies heavily on the project SMOP by Victor Leikehman. The current implementation wraps around SMOP, with the following differences:
- It attempts to produce code that does not rely on
libsmop
, but only on usual python modules such asnumpy
. - It uses typical shortcuts such as
np
instead ofnumpy
. - It attemps to reindex arrays and loops, starting from 0 instead of 1.
- It doesn't use the external classes
matlabarray
andcellarray
fromlibsmop
- Basic support for Matlab classes is added. The properties declared in the body of the class are initialized in the constructor.
- As a consequenc of all the above, the resulting code is "less safe" but maybe slightly closer to what a user would write.
This implementation is made straightforward, since it basically use another backend script than the one used by SMOP, here called smop\backend_m2py.py
.
Some function replacements were added directly there.
Additional support for classes, import modules and other fine-tuning replacements (or hacks...) are done in the file matlabparser\parser.py
.
The code is written in python, you can access it as follows:
git clone https://github.com/ebranlard/matlab2python
cd matlab2python
# install dependencies
python -m pip install --user -r requirements.txt
# make the packages available from any directory
python -m pip install -e .
#Optional: run tests
pytest
#Optional: try it directly
python matlab2python.py tests/files/fSpectrum.m -o fSpectrum.py
The main script at the root of the repository is executable and has a couple of command line flags (some of them taken directly from SMOP).
To convert the file file.m
to file.py
, simply type:
python path/to/matlab2python.py file.m -o file.py
where path/to
is the path to the script matlab2python.py
The python package can also be used directly to perform conversion of files or lines of code.
import matlabparser as mpars
# --- Convert a matlab file
pylines = mpars.matlab2python('path/to/file.m', output='file.py')
# --- Convert matlab lines (string or list of strings)
mlines="""# a comment
x = linspace(0,1,100);
y = cos(x) + x**2;
"""
pylines = mpars.matlablines2python(mlines, output='stdout')
If you need a script that performs the obvious conversions from matlab to python, matlab2python
will hopefully work for you.
These conversions are for instance:
- syntax (
def
,if
,for
,__init__
, no moreend
) - indentation
- parenthesis to brackets
- simple builtin functions replacements (
fprintf
,disp
,error
,fopen
) - simple numpy replacements like
zeros(3,4)
tonp.zeros((3,4))
, orcosd(x)
, tonp.cosd(np.pi/180 x)
- other misc functions like
strcmp
,strrep
,reshape
replaced by their python , - etc
As mentioned above, SMOP does a great job to produce safe code.
Yet, neither SMOP
nor matlab2python
will generate code that is production-ready (it might in some cases).
Most of the time, the user will have to go through the code and perform adjustements and some rewritting.
In fact, matlab2python
will likely be slightly worse than SMOP in producing a code that works out of the box.
But at the end, the code produced by matlab2python
should require less refactoring and help the user in its conversion.
As mentioned by the author of SMOP, it is difficult not to hide the matlab flavor from the code that is generated and it's also difficult to fully convert the code without introducing wrapped classes such that matlabarray
. The implemenation of matlab2python
attempts to do that, at the price of less safety.
I've written this wrapper script for my own needs. I was ready to convert manually a bunch of matlab scripts, but I thought I could have a script to automate some of the simple conversions and formatting. I started a quick and dirty implementation before discovering SMOP
. At the end, I merged my quick and dirty implemenation with the more powerful parsing framework used by SMOP. Hopefully this can be useful to someone else! If so, feel free to contribute.
Any contributions to this project are welcome! If you find this project useful, you can also buy me a coffee (donate a small amount) with the link below: