[![Build status](https://github.com/ebranlard/matlab2python/workflows/Tests/badge.svg)](https://github.com/ebranlard/matlab2python/actions?query=workflow%3A%22Tests%22)
<a href="https://www.buymeacoffee.com/hTpOQGl" rel="nofollow"><img alt="Donate just a small amount, buy me a coffee!" src="https://warehouse-camo.cmh1.psfhosted.org/1c939ba1227996b87bb03cf029c14821eab9ad91/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f6e6174652d4275792532306d6525323061253230636f666665652d79656c6c6f77677265656e2e737667"></a>
# 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](http://github.com/victorlei/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:
```bash
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:
```bash
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.
```python
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](http://github.com/victorlei/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:
<a href="https://www.buymeacoffee.com/hTpOQGl" rel="nofollow"><img alt="Donate just a small amount, buy me a coffee!" src="https://warehouse-camo.cmh1.psfhosted.org/1c939ba1227996b87bb03cf029c14821eab9ad91/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f6e6174652d4275792532306d6525323061253230636f666665652d79656c6c6f77677265656e2e737667"></a>
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Matlab和Python都是非常流行的编程语言。如果您想将Matlab代码转换为Python代码,可以采取以下步骤: 1. 了解两种语言的语法和语义差异。Matlab和Python有很多不同之处,例如变量类型、函数定义和语句结构等。 承接可视化、数据分析、编程辅导js,py,d3,echarts 2. 使用MATLAB引擎 for Python。这是一个MATLAB的Python接口,可以在Python中调用MATLAB代码和函数。您可以使用该接口将MATLAB代码嵌入到Python程序中。 3. 逐行将MATLAB代码转换为Python代码。这需要一些手动工作,但是可以确保代码的正确性和可读性。 4. 使用现有的MATLAB转换工具。有一些MATLAB转换工具可以将MATLAB代码转换为Python代码。这些工具包括m2py和Matlab2Python。 请注意,将MATLAB代码转换为Python代码可能需要一些时间和努力,因为两种语言的语法和语义存在差异。因此,您应该仔细检查和测试转换后的代码,以确保其正确性和可靠性。
资源推荐
资源详情
资源评论
收起资源包目录
matlab to python.rar (40个子文件)
matlab to python
matlab2python-main
smop
__init__.py 98B
version.py 24B
README 9KB
main.py 825B
README.rst 12KB
node.py 11KB
resolve.py 7KB
LICENSE.MIT 1KB
parsetab.py 91KB
backend.py 11KB
options.py 4KB
backend_m2py.py 21KB
libsmop.py 5KB
rewrite.py 8KB
parse.py 21KB
recipes.py 6KB
lexer.py 11KB
setup.py 541B
.github
workflows
tests.yml 2KB
Makefile 1KB
tests
__init__.py 0B
files
__init__.py 1B
fSpectrum.m 161B
run_all.m 277B
ChildClass.m 395B
TestClass.m 415B
test_function1.m 528B
test_parser.py 3KB
test_common.py 2KB
test_files.py 4KB
test_parsingtools.py 2KB
matlab2python.py 4KB
matlabparser
__init__.py 134B
parser.py 19KB
parsing_tools.py 6KB
LICENSE.TXT 1KB
requirements.txt 47B
.gitignore 57B
README.md 5KB
MATLAB百度百科.txt 1KB
共 40 条
- 1
资源评论
野生的狒狒
- 粉丝: 1675
- 资源: 1668
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功