Coder Social home page Coder Social logo

matlab2python's Introduction

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
python -m pip install --user -r requirements.txt

Usage

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 matlab2python.py file.m -o file.py

The python package can also be used directly to perform conversion of files or lines of code.

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!

matlab2python's People

Contributors

ebranlard avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.