Overview
Whether or not you are using Matlab you are still coding so why not use python, its Free! Python is an interpreter language that have high level abstractions to make coding easy, well, easier and if you coming from Matlab, most of the scripting skill you already have is translatable. This wiki space is dedicated to making Python coding easy with resources and link to resources that you will/may need to write awesome python programs.
Python and its modules
For coding in python like matlab you will need (the bare minimum):
- Python (of course), preferably Python 3.6 or above. Python 2.7 will no longer be supported by the developers.
- Numpy
- Scipy
- Matplotlib
- pandas
- A text editor i.e. notepad++
To make things easier, the following software packages will help manage and develop your scripts/programs:
- Anaconda Distribution
Anaconda is an open-source distribution of python, that provides and manages the commonly used packages for data science and machine learning so you don't have to.
Currently (in 2019) we recommend python 3.6, because it is the most compatible version across different OS especially for Ubuntu 18.04 LT.
To install the latest python 3.6 using anaconda, download Anaconda3-5.2.0 from https://repo.anaconda.com/archive/.
Otherwise if compatibility is not important to you then the default Anaconda3 Distribution will do (https://www.anaconda.com/distribution/). - Integrated development environment (IDE): pycharm IDE community addition (Recommended). This is to make your coding life easier.
Differences Between Matlab and Python
There are a lot of differences between Matlab and Python beside the fact that Python is free. The following is a list of differences you need to be aware of when switch from Matlab to Python.
- Python indexing starts at Zero
- Python uses square brackets [ ] instead of ( ) to for indexing arrays
- To perform matrix and array operation you need to import the numpy module in python
- There are other data structures in Python like dictionary
- You don't need to put a semi-colon at the end of your statement in Python
- In Python, white-space matters. Python use these white-spaces to define the scope of your code. Matlab use the "end" key word i.e. in the end of loops while Python does not.
Python have functions and methods. Functions are similar to Matlab functions, the way they work is the same, the way it is call is the the same but the way they are defined is a bit different.
Matlabfunction output = myfunc(arg) ...
Pythondef myfunc(arg): output = 1 return output
Methods on the hand is also associated with an object:
Python methodclass MyObj(object): def myfunc(self, arg): output = 1 return output
To call a method you have to do this
Python method callingmyobj = MyObj() ret = myobj.myfunc(0)
To plot graphs in python use matplotlib
Plotimport matplotlib.pyplot as plt from faze2.part1.model.gait import SpatioTemporal ipu = SpatioTemporal() csvio = csv_IO("../matlab/Faze2/Overground_walking_acc_smoot.csv") data_acc = csvio.read() data_acc = [[float(data_acc[i][j]) for j in range(3, 4)] for i in range(0, len(data_acc))] x = np.array(data_acc[50:][:]) x = np.squeeze(np.transpose(x)) peaks = ipu.__search_for_maxs__(x) plt.plot(x) plt.plot(peaks, x[peaks], "x") plt.show()
Output:
Multiplying Matrix
Matrix multiplicationimport numpy as np a = np.array([[3, 2, 1], [1, 2, 4], [2, 1, 3]]) b = np.array([[1, 2, 3], [3, 2, 1], [2, 1, 3]]) c = np.dot(a, b) print(c)
Console Output[[11 11 14] [15 10 17] [11 9 16]]
- Python
Useful tips and tricks
To find out what is your current working dir:
Pythonimport os cwd = os.getcwd()
To find out what the real path of your relative path:
Pythonimport os dir_path = os.path.realpath("../../../../matlab/Faze2/Overground_walking_acc_smoot.csv")
Setting Up Your Python Environment
The easiest way to setup Python is to install Anaconda and keeping the options as default. Once it is installed you can check the installing by either opening command prompt or anaconda prompt. Then in the window type: python or py and it should show the python scripting interface.
Next, install pycharm. Once installed it should look like this when you created a project:
It should automatically detect your python install. If not you can add it to your project by going into settings
Then to project: <name of your project> and Open Project Interpreter
Open drop down and click on show all ...
If the python interpreter is not shown, you can try adding it:
There is two options here, to simplify things we will just add an existing interpreter by first navigating to your python installation. Then press ok.
Then you are done and have python ready to go.
Recent space activity
Space contributors