Child pages
  • Don't Panic: A Hitchhiker's Guide to Freeing yourself from Matlab with Python

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Div
classhome-banner

Time is an illusion. Lunchtime doubly so.




Div
classtitle-box

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.



Div
classtitle-box

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.



Div
classtitle-box

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.

    Code Block
    languagejs
    themeRDark
    titleMatlab
    function output = myfunc(arg)
      ...


    Code Block
    languagepy
    themeRDark
    titlePython
    def myfunc(arg):
        output  = 1
        return output 

    Methods on the hand is also associated with an object:

    Code Block
    languagepy
    themeRDark
    titlePython method
    class MyObj(object):
    
        def myfunc(self, arg):
    
            output = 1
    
            return output 

    To call a method you have to do this

    Code Block
    languagepy
    themeRDark
    titlePython method calling
    myobj = MyObj()
    ret = myobj.myfunc(0)


  8. To plot graphs in python use matplotlib

    Code Block
    languagepy
    themeRDark
    titlePlot
    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 = self.csvio.read()
    data_acc = [[float(self.data_acc[i][j]) for j in range(3, 4)] for i in range(0, len(self.data_acc))]
    x = np.array(self.data_acc[50:][:])
    x = np.squeeze(np.transpose(x))
    peaks = self.ipu.__search_for_maxs__(x)
    
    plt.plot(x)
    plt.plot(peaks, x[peaks], "x")
    plt.show()

    Output:


  9. Multiplying Matrix

    Code Block
    languagepy
    themeRDark
    titleMatrix 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)
    
    


    Code Block
    languagepowershell
    themeEmacs
    titleConsole Output
    [[11 11 14]
     [15 10 17]
     [11  9 16]]


  10. Python




Div
classtitle-box

Useful tips and tricks

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

    Code Block
    languagepy
    themeRDark
    titlePython
    import os
    
    
    cwd = os.getcwd()


  2. To find out what the real path of your relative path:

    Code Block
    languagepy
    themeRDark
    titlePython
    import os
    
    
    dir_path = os.path.realpath("../../../../matlab/Faze2/Overground_walking_acc_smoot.csv")




Div
classtitle-box

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.



Div
classtitle-box



Div
classtitle-box

Recent space activity

Recently Updated
typespage, comment, blogpost
max5
hideHeadingtrue
themesocial



Div
classtitle-box

Space contributors

Contributors
modelist
scopedescendants
limit5
showLastTimetrue
orderupdate


 

CSS Stylesheet
.home-banner {
                    background: #0052cc;
                    color: #fff;
                    font-size: 20px;
                    padding: 20px;
                    }
                    .home-banner h2 {
                    color: #fff;
                    }
                    .title-box {
                    border: 1px solid #0052cc;
                    padding: 10px;
                    }
                    .title-box > h2 {
                    background: #0052cc;
                    bottom: 10px;
                    color: #fff;
                    margin-left: -10px;
                    margin-right: -10px;
                    padding: 2px 10px;
                    position: relative;
                    }