MATLAB to Python Code conversion (NumPy, SciPy, MatplotLib?)

后端 未结 3 1103
青春惊慌失措
青春惊慌失措 2021-01-06 09:22

I\'m trying to convert the following code to Python from MATLAB for an EEG Project (partly because Python\'s slightly cheaper!)

Hopefully someone can point me in the

相关标签:
3条回答
  • 2021-01-06 09:43

    This may or may not work, but you might want to try a Matlab to Python converter such as mat2py. I've never tried them, but it might save some time. Also, there's this page about Matlab to Numpy conversion that might help you get acquainted with the differences between the two.

    0 讨论(0)
  • 2021-01-06 09:54

    One small point - indexing between the two is different. If you just copy everything from MATLAB to Python, as you seem to have done, you'll be very very confused. MATLAB x(1:5:end) translates to Python x[0::5]. Go back to NumPy for MATLAB Users and scan down to the section called "Linear Algebra Equivalents" (about halfway down the page). It gives a dictionary of how to go back and forth.

    0 讨论(0)
  • 2021-01-06 10:02

    Um... lots of things.

    Python has no end keyword, so you clearly need to read more about Python's syntax.

    Python arrays and slices are indexed with [] not (). Ranges are expressed as range(0,10) for example, but slices in the Matlab sense only exist in extension packages like numpy and each one has its own interface.

    Yes, you want to use matplotlib for plotting, it has pretty much the same capabilities as Matlab's plotting interface, at least at this level.

    It looks like you're guessing that Python will have the same method names as Matlab in some random package. This is not a good plan. Instead, look up the Matlab method in its documentation, which is online, find out exactly what it does, and then read into the Python package documentation for a method that does what you want. This may not exist, but I'm betting that in a program this simple, most of the ones you need will.

    The most important thing you need to understand in converting any Matlab code to other languages is the way Matlab arrays work, which is extremely unusual (but excellent for its target applications). Numpy has about the same capabilities, but a completely different notation for them.

    The serial module has already given you an open file object on the port, so you don't need the fopen.

    I think you need to spend a LOT of time with the documentation for both Python and Matlab, because it's quite clear you understand neither at the moment.

    Don't let me discourage you, I'm just being honest about where you're at.

    0 讨论(0)
提交回复
热议问题