Python integration in MATLAB#

Currently, some functionalities in NeuroMiner use the MATLAB Interface to Python to take advantage of Python’s data science libraries. Thus, in those cases, a Python installation may be required, as well as setting up the Python interpreter in MATLAB - this is letting MATLAB know where the Python interpreter is installed in your machine. Users are recommended to follow the MATLAB documentation to install and set the Python Environment in MATLAB. The documentation provides the steps to follow depending on the OS used. Briefly, we will describe steps here that should work across multiple OS:

  1. Install a MATLAB compatible version of Python if you do not have one already installed.

Python can be installed in different ways, on its own or as part of a conda installation. We recommend installing it on its own for easiness of use and to be able to install a version of Python compatible with your MATLAB version. If installing using a conda environment, it is recommended to create a dedicated environment for the Python interpreter used in MATLAB to avoid compatibility issues, since conda usually pre-installs most data science libraries. Python installations using the Microsoft Store are not recommended.

  1. Install the required libraries

To install the necessary Python packages, open the command prompt from the NeuroMiner main directory and run the following command:

pip install -r Python/requirements.txt

The “requirements.txt” is a file with all the required libraries to use Python in NeuroMiner. Alternatively, you can install them separately, the libraries required by NeuroMiner are the following:

The scikit-learn library includes other data science libraries (pandas, scipy and numpy) as dependencies. If the pip command returns an error such as “pip is not recognized as an internal or external command”, it is possible that your Python interpreter path is not set in your PATH. Follow this documentation to set your Python interpreter to PATH in Windows OS. Analogous documentation can be found online for other OS. Bear in mind that if you are using Conda, your Python executable will be within the Conda folder and enviroment. Once you have added Python as an environment variable, close the terminal and open again, and try the command above again.

  1. Connect the Python interpreter to MATLAB

The last step is to let MATLAB know where the Python interpreter is. In most cases, MATLAB can automatically identify where Python is in your machine. In the MATLAB command window, execute:

pe = pyenv

If the output is something as the following:

pe =

  PythonEnvironment with properties:

          Version: "3.10"
       Executable: "C:\Users\username\AppData\Local\Programs\Python\Python310\pythonw.exe"
          Library: "C:\Users\username\AppData\Local\Programs\Python\Python310\python310.dll"
             Home: "C:\Users\username\AppData\Local\Programs\Python\Python310"
           Status: NotLoaded
    ExecutionMode: InProcess

or as the following:

pe =

  PythonEnvironment with properties:

          Version: "3.9"
       Executable: "/opt/anaconda3/bin/python"
          Library: "/opt/anaconda3/lib/libpython3.9.so"
             Home: "/opt/anaconda3"
           Status: NotLoaded
    ExecutionMode: InProcess

It means MATLAB is detected your Python interpreter. If the PythonEnviroment variable has empty fields, you will need to set it manually. Locate the path of your Python interpreter, and in the MATLAB command window execute the following command to link the interpreter to MATLAB:

pyenv("PATH_TO_PYTHON_EXECUTABLE")

You can check that the link to the Python interpreter has been established by running a simple command in the MATLAB command window such as:

py.list([1,2,3])

This should return a Python list stored as a variable in MATLAB. As a sanity check, users should also check that the required libraries are recognized by MATLAB, by importing modules with the following command:

py.importlib.import_module('sklearn');

If this command returns an error, either the sklearn library is not installed, or MATLAB is not recognising it. you can install libraries in Python from MATLAB by typing:

system('PATH_TO_PYTHON_EXECUTABLE -r pip install scikit-learn')

After that, try to import sklearn again using the above command. If errors persist, please create an issue in the NeuroMiner Github repository for further assistance.