Convert MATLAB Codes into Python

Is there a way to batch convert a folder of MATLAB files into Python files?

Moving this from ideas to help, as it seems more like asking for help than ideas for improving Python.

What do you mean by “convert”? What should happen when you do this? By “matlab file”, do you mean code, data or something else?

No, you cannot expect the computer to convert from code written in one programming language to another. If that were possible for a machine, and if the result were of any value, one of those two programming languages would not exist.

Be aware of the issues mentioned in this guide: NumPy for MATLAB users — NumPy v1.26 Manual

Good luck, if you try this: GitHub - victorlei/smop: Small Matlab to Python compiler or another idea from this answer A tool to convert MATLAB code to Python - Stack Overflow

2 Likes

Interesting project - I took a very quick look and saw that a main underlying library is ply, a Lex/Yacc implementation in Python by David Beazley. It’s a a really nice compiler-creation library in pure Python.
(PLY (Python Lex-Yacc)) (followed up by Sly)

1 Like

Thanks @kknechtel Karl. I mean by convert to mainly convert MATLAB codes.

That doesn’t even remotely answer the question. It simply tells me again the one thing I already understood, and ignores everything I was asking about.

2 Likes

Isn’t this exactly what a complier does (i.e. C to assembly)?

Yes, C is converted to assembly by the compiler as you stated (this is how you then get HEX files). But C to assembly conversion is one step in a needed process of converting a high-level English language program to machine useable code (usually you can get the assembly code by selecting the Dissasembly option in an IDE). But this comparison does not directly apply to what the OP was asking (MATLAB to Python).

First note that assembly (instruction set) tends to be manufacturer specific (though the general idea holds). If you reference programming manuals for different manufactures for their respected microcontroller product lines, and compare them, you’ll see that there exists slight differences in their naming conventions for many instructions. That is, assembly may vary from one mfg. to another and thus is not a general language per se (Python does not vary from user to user). It is generally tied to actual hardware (i.e., a particular mfg. and microcontroller family).

So, yes, C is converted to assembly, but with some nuances.