how to run python in matlab

2 visualizaciones (últimos 30 días)
Joseph
Joseph el 31 de Oct. de 2021
Comentada: Joseph el 3 de Nov. de 2021
I would like to run some python script (namely the mne package for analyzing EEG data) from Matlab 2021b under Ubuntu 21.04.
While I see various postings saying this is simple, I am not finding that this is the case.
I have python and mne successfully installed and it runs fine via the Terminal app.
I have tried writing a .py file and running it from Matlab, as in system('python3 temp.py')
It doesn't work. From what I've found online, Matlab is running it's own shell and in my case mne is not installed in it. It's not even running the same version of python. I've got python 3.8 in the Terminal but Matlab is running python 3.9, so when I run a .py file with:
import sys
print(sys.path)
I get: ['/media/jdien/Data1/experiments/NP1/artifactPaper/MAAC', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages']
I can't find any information on how to add mne to the path or to get it installed into Matlab's apparently private environment.
If I try running a .py file with:
apt-get install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6
pip install mne
I just get the error message:
apt-get install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6
^
SyntaxError: invalid syntax
I've also tried using Matlab's built-in Python functionality, as described at https://www.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html
Here, if I try something simple like: >> py.sys.path
I get the error message:
Python commands require a supported version of CPython. See Getting Started with
Python.
So here even Python is not installed and again I seem to have no way to get it installed, let alone mne.
Can someone help me with a more useful response than that it is easy to run python from matlab? Sorry, a little frustrated here...
Apparently I should also clarify that I used anaconda to install python so that may be part of what is causing problems. But still not clear what to do. Here is what I get when I do the sys.path command in my Terminal app:
['/media/jdien/Data1/experiments/NP1/artifactPaper/MAAC', '/home/jdien/anaconda3/lib/python38.zip', '/home/jdien/anaconda3/lib/python3.8', '/home/jdien/anaconda3/lib/python3.8/lib-dynload', '/home/jdien/.local/lib/python3.8/site-packages', '/home/jdien/anaconda3/lib/python3.8/site-packages']

Respuestas (1)

Joseph
Joseph el 31 de Oct. de 2021
Ah, I figured it out! I needed to reassign the Matlab shell to use the anaconda version, which includes mne, by using the system command at the Matlab command line to invoke update-alternatives:
https://phoenixnap.com/kb/upgrade-python
  1 comentario
Joseph
Joseph el 3 de Nov. de 2021
Oops! do NOT do this! it turns out that this command changes the version the entire system uses, not just Matlab, by changing the symbolic link at /usr/local/python3 to point towards the anaconda version (via /etc/alternatives), and at least in my case caused various things to stop working. I'm going to keep looking for a solution.

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by