Child pages
  • Don't Panic: A Hitchhiker's Guide to Freeing yourself from Matlab with Python
Skip to end of metadata
Go to start of metadata

Time is an illusion. Lunchtime doubly so.


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):

  1. Python (of course), preferably Python 3.6 or above. Python 2.7 will no longer be supported by the developers.
  2. Numpy
  3. Scipy
  4. Matplotlib
  5. pandas 
  6. A text editor i.e. notepad++

To make things easier, the following software packages will help manage and develop your scripts/programs:

  1. 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/).

  2. 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.

  1. Python indexing starts at Zero
  2. Python uses square brackets [ ] instead of ( ) to for indexing arrays
  3. To perform matrix and array operation you need to import the numpy module in python
  4. There are other data structures in Python like dictionary
  5. You don't need to put a semi-colon at the end of your statement in Python
  6. 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.
  7. 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.

    Matlab
    function output = myfunc(arg)
      ...
    Python
    def myfunc(arg):
        output  = 1
        return output 

    Methods on the hand is also associated with an object:

    Python method
    class MyObj(object):
    
        def myfunc(self, arg):
    
            output = 1
    
            return output 

    To call a method you have to do this

    Python method calling
    myobj = MyObj()
    ret = myobj.myfunc(0)
  8. To plot graphs in python use matplotlib

    Plot
    import 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:

  9. Multiplying Matrix

    Matrix multiplication
    import 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]]
  10. Python



Useful tips and tricks

  1. To find out what is your current working dir:

    Python
    import os
    
    
    cwd = os.getcwd()
  2. To find out what the real path of your relative path:

    Python
    import 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

{"mode":"list","scope":"descendants","limit":"5","showLastTime":"true","order":"update","contextEntityId":158145447}