[![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脚本
共39个文件
py:25个
m:5个
txt:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 161 浏览量
2022-11-09
22:56:26
上传
评论
收藏 72KB ZIP 举报
温馨提示
matlab脚本转化为Python脚本
资源推荐
资源详情
资源评论
收起资源包目录
matlab2python-main.zip (39个子文件)
matlab2python-main
matlabparser
parser.py 19KB
parsing_tools.py 6KB
__init__.py 134B
.github
workflows
tests.yml 2KB
tests
test_function1.m 528B
test_files.py 4KB
test_parser.py 3KB
test_parsingtools.py 2KB
__init__.py 0B
test_common.py 2KB
files
fSpectrum.m 161B
ChildClass.m 395B
__init__.py 1B
TestClass.m 415B
run_all.m 277B
LICENSE.TXT 1KB
smop
README.rst 12KB
resolve.py 7KB
options.py 4KB
main.py 825B
LICENSE.MIT 1KB
lexer.py 11KB
backend_m2py.py 21KB
recipes.py 6KB
parsetab.py 91KB
rewrite.py 8KB
__init__.py 98B
README 9KB
parse.py 21KB
node.py 11KB
backend.py 11KB
version.py 24B
libsmop.py 5KB
requirements.txt 47B
matlab2python.py 4KB
setup.py 541B
.gitignore 57B
Makefile 1KB
README.md 5KB
共 39 条
- 1
资源评论
小码蚁.
- 粉丝: 2520
- 资源: 4067
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- html动态爱心代码一(附源码)
- c40539bc-071a-486c-9d52-9d0c18d62dac 4.html
- 基于物理的非视域成像(NLOS)算法,利用了nerf+python源码+文档说明
- yuluer知更鸟.7z(1).001
- python课程设计-基于tensorflow实现的图文生成程序,数据集flickr30k-images+源代码+文档说明+截图
- python作业-基于Flickr30k数据集实现图像文本跨模态搜索python源码+数据集+测试界面+项目说明(高分课程设计)
- 基于Qt实现医院信息管理系统c++源码+文档说明+数据库(期末大作业)
- 基于python实现的医院信息管理系统完整源码+sql数据库+详细注释(高分课程设计)
- 基于python的眼底图像视杯视盘分割项目源码+文档说明+截图演示+详细注释(高分课程设计)
- ImageBasedModellingEdu-贰壹贰叁零
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功