• Stars
    star
    262
  • Rank 156,136 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple matlab2python converter

Build status Donate just a small amount, buy me a coffee!

matlab2python

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 as numpy.
  • It uses typical shortcuts such as np instead of numpy.
  • It attemps to reindex arrays and loops, starting from 0 instead of 1.
  • It doesn't use the external classes matlabarray and cellarray from libsmop
  • 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.

Install

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

Usage

Main script

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

From python

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

Should I use this

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 more end)
  • indentation
  • parenthesis to brackets
  • simple builtin functions replacements (fprintf, disp, error, fopen)
  • simple numpy replacements like zeros(3,4) to np.zeros((3,4)), or cosd(x), to np.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.

Contributing

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:

Donate just a small amount, buy me a coffee!

More Repositories

1

pyDatView

A crossplatform GUI to plot tabulated data from files (e.g. CSV, Excel, OpenFAST, HAWC2, Flex...), or python pandas dataframes
Python
93
star
2

matlab2fortran

Performs some simple conversions from matlab code to fortran
MATLAB
63
star
3

welib

Wind energy library, python and matlab tools for wind turbines analyses
Python
54
star
4

vim-matlab-behave

Reproduce matlab editor behavior (e.g. run functionality)
Vim Script
30
star
5

xdotool-for-windows

Command line implementation of xdotool for windows (pseudo-equivalent)
Roff
26
star
6

wtDigiTwin

A wind turbine digital twin based on YAMS
Python
24
star
7

weio

Library to read and write files (in particular files used in the Wind Energy community)
Python
23
star
8

fortran-guidelines

Guidelines for programming in fortran
Fortran
18
star
9

wmctrl-for-windows

Command line implementation of wmctrl for windows (pseudo-equivalent)
C#
16
star
10

YAMS

Yet Another Multibody Solver - Structural dynamics tools, beam theory, FEM and more
Python
6
star
11

simple-fortran-parser

fortran parser to automatically generate derived type source code and generate library call header
Python
5
star
12

viz3Danim

Visualize 3D animation and mode shapes in a web GUI
JavaScript
5
star
13

par-latex

Reformat latex document or paragraphs
Python
3
star
14

wiz

Wake and induction zone models for wind farm calculations
Python
3
star
15

pyviz3danim

Python app to visualize 3D structures, mode shapes and animations.
Python
2
star
16

aerobench

Python
2
star
17

matlab-path

Matlab tools for exporting figures, exporting to latex, debugging, testing, plotting, and misc.
MATLAB
1
star
18

mlstocks

Tools for investing
Python
1
star
19

wtDigiTwin-data

Python
1
star
20

vim-simple-comment

Simple comment plugin for vim - supports toggle comment, block comments, and box comments
Vim Script
1
star