Clear Filters
Clear Filters

Python - MATLAB not working: MATLAB cant find python environment, numpy not working, fooof not working

51 views (last 30 days)
Hi everyone,
I've been stuck on trying to run some matlab-python code for a week now and nothing seems to work. I am trying to run the following code: https://github.com/fooof-tools/fooof_mat?tab=readme-ov-file which requires python to be compatible with MATLAB. I've read so many things but nothing has worked so far. This is my first time trying to code in python-matlab simultaneously so the answer might be something simple that I am just not aware of.
What has worked so far:
I created a new environment called 'env' which is located in location 'fri_test'. In this environment I have installed fooof, matlibplot, pandas.
Then, I open a fresh MATLAB window and type in the following:
pyenv
This is the output:
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/Users/c1034944/fri_test/env/bin/python"
Library: "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/libpython3.9.dylib"
Home: "/Users/c1034944/fri_test/env"
Status: NotLoaded
ExecutionMode: InProcess
I then run:
python_env_path = '/Users/c1034944/fri_test/env/bin/python'
pyenv("Version",python_env_path);
py.list({'This','is a','list'})
output:
ans =
Python list with no properties.
['This', 'is a', 'list']
Lastly, run this again to check the python environment:
pyenv
output:
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/Users/c1034944/fri_test/env/bin/python"
Library: "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/libpython3.9.dylib"
Home: "/Users/c1034944/fri_test/env"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "42704"
ProcessName: "MATLAB"
As you can see the 'executable' is finding the correct location of the environment I created in fri_test. The 'ExecutionMode' has also now updated to 'InProcess'. However, the 'Library" is still calling the python version from the Library/Developer.
To test if numpy is working, I run the following:
freq=([0 0.9766 1.9531 2.9297])
py.numpy.array(freq)
this is the error message:
Unable to resolve the name py.numpy.array.
More info: The 'fooof' code I am trying to run contains the following line: py.numpy.array(freqs), where 'freqs' is a list of 1 x 257 numbers. To my understanding py.numpy.array(freqs) converts the matlab (.mat) file 'freqs' into a python-compatible array.
Conclusion: I have no idea what is the issue here and why numpy is not working. I know that MATLAB and python versions are compatible, but maybe I am making a different mistake. I am hoping that finding a solution to numpy problem and that may fix the whole issue - is there any other way to convert .mat files into python arrays?
Any suggestions would be helpful!
Healthy Regards,
Vyte
  13 Comments
Vyte Jan
Vyte Jan on 14 Oct 2024 at 17:23
Thank you for your insightful feedback!
As you say, I believe I have followed the steps accurately and as far as MATLAB and the virtual environment goes they have both been set up correctly.
I know for sure that MATLAB2021, python3.9 and numpy2.0.2 versions are compatible. I also tried the steps with MATLAB2024 and python3.12 but I get the same error (numpy being loaded from source tree). As these being the newer versions of both; matlab and python - I expected there to be less comments online about solving issues.
As I only have six weeks left on my project I cannot spend anymore time on this, but once again I really appreciate your time. I was able to learn a lot about python, virtual env & the relationship between matlab and python.
Thank you very much!
Umar
Umar on 15 Oct 2024 at 1:56
Hi @Vyte Jan,
Thank you for your detailed response and for sharing your experience with the setup process. I appreciate your thoroughness in confirming the compatibility of MATLAB 2021, Python 3.9, and NumPy 2.0.2, as well as your efforts to troubleshoot with the newer versions. I understand that time is of the essence with only six weeks remaining on your project. It’s great to hear that despite these challenges, you have gained valuable insights into Python, virtual environments, and their integration with MATLAB. If there are any specific issues or errors you would like further assistance with before concluding your project, please feel free to reach out. I am here to help in any way I can. Thank you once again for your dedication and hard work.

Sign in to comment.

Answers (1)

Umar
Umar on 29 Sep 2024

Hi @Vyte Jan,

After going through your comments and reviewing the documentations provided at the links below,

https://www.mathworks.com/matlabcentral/answers/621078-converting-numpy-arrays-in-python-to-mat-in-matlab

https://www.mathworks.com/matlabcentral/answers/621078-converting-numpy-arrays-in-python-to-mat-in-matlab

https://www.mathworks.com/help/matlab/matlab_external/get-started-with-matlab-engine-for-python.html

https://www.matlabexpo.com/content/dam/mathworks/mathworks-dot-com/images/events/matlabexpo/online/2022/python-for-matlab-development.pdf

https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html

https://www.mathworks.com/matlabcentral/answers/427187-problem-with-python-numpy?s_tid=mwa_osa_a

https://www.mathworks.com/help/matlab/matlab_external/passing-data-to-python.html

Please see suggested solutions listed below.

Verify NumPy Installation

  • First, ensure that NumPy is indeed installed in your specified Python environment. You can do this by activating your environment and running:
         python -m pip show numpy
  • If it's not installed, you can install it using:
         python -m pip install numpy

Check Python Environment Path

  • Make sure that MATLAB is pointing to the correct Python executable. You’ve already set this using:
          pyenv("Version", python_env_path);
  • Confirm that the environment is loaded properly by checking `pyenv` output after this command.

Using py.importlib.import_module

  • Before calling any NumPy functions, explicitly import NumPy in MATLAB:
        np = py.importlib.import_module('numpy');
       Then, try calling NumPy functions like so:
         freq = [0 0.9766 1.9531 2.9297];
         py_array = np.array(freq);

Direct Conversion Alternatives

  • If you continue to face issues with `py.numpy.array()`, consider converting your data into a compatible format without direct reliance on NumPy. For instance, you can convert MATLAB arrays into Python lists:
        freq_list = py.list(num2cell(freq)); % Convert MATLAB array to cell array   
        then to Python list

Please see attached.

Use of pyrun or pyrunfile Functions

  • If you have a more complex script or if you need to execute multiple lines of Python code, consider using `pyrun` or `pyrunfile` functions which allow you to run entire scripts and handle outputs effectively.

Also, please review the documentations provided in the links above as well.

Hope following these suggestions should help resolve your problems. Please let me know if you have any further questions.

Community Treasure Hunt

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

Start Hunting!