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

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

Hi @Vyte Jan,
Please let me know if you need any further assistance, will be more happy to help you out.
Thank you very much for your long message and apologies for the late reply.
I'm glad to hear I was on the right path!
I've been stuck on a new error; confirming the environment after the running:
py.importlib.import_module
I get the following error:
>> py.importlib.import_module
Python Error: TypeError: import_module() missing 1 required positional argument: 'name'
I went back to PyCharm where I have my env opened and I tried running:
pip install importlib
this is the following error message on PyCharm terminal:
Collecting importlib
Downloading importlib-1.0.4.zip (7.1 kB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
exit code: 1
╰─> [1 lines of output]
ERROR: Can not execute `setup.py` since setuptools is not available in the build environment.
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
I can't seem to fix this issue, not sure what I've done wrong here.
In the meantime, I am trying to avoid the numpy function and see if a direct conversion might work.

Hi @Vyte Jan ,

Sorry for late response since busy working with clients all day long. I reviewed your comments Your query revolves around two main issues:

1. An error related to the importlib.import_module function in Python, specifically a TypeError indicating that a required argument is missing.

2. An error encountered while trying to install the importlib package via pip in PyCharm, highlighting issues with the build environment, specifically related to setuptools.

Let me address these issues

TypeError with importlib.import_module

The error message you received:

TypeError: import_module() missing 1 required positional argument: 'name'

indicates that you are calling importlib.import_module without providing the necessary argument. The import_module function requires a string argument that specifies the name of the module you want to import. Here's how you can correctly use it:

import importlib
# Correctly importing a module named 'math'
math_module = importlib.import_module('math')
# Now you can use math functions
print(math_module.sqrt(16))  # Output: 4.0

Make sure you replace math with the actual module name you intend to import. The string should be the exact name of the module as it would be referenced in an import statement.

Error Installing importlib

Regarding the error when attempting to install importlib, it is important to note that importlib is part of the Python standard library starting from Python 3.1, so there is generally no need to install it separately via pip. The error message indicates that you're trying to install a package that should already be included with your Python installation. However, if you're encountering issues with setuptools, it suggests that your Python environment might be misconfigured. Here’s how to address this:

Fixing the Setuptools Issue

1. Make sure you have Setuptools Installed

Run the following command in your terminal or PyCharm terminal:

     pip install --upgrade setuptools

2. Check Your Python Environment

Make sure that your virtual environment is activated properly. If you're using a virtual environment, activate it before running pip commands:

     .\venv\Scripts\activate

3. Reinstall Pip

If problems persist, consider reinstalling pip:

   python -m ensurepip --upgrade

4. Creating a New Virtual Environment

If none of the above steps resolve your issue, creating a new virtual environment might help:

   python -m venv new_env_name
   source new_env_name/bin/activate 

Direct Conversion vs Numpy

If you're considering avoiding numpy for conversions, ensure that your data types are compatible and use native Python functions where applicable. For example, converting lists to tuples can simply be done using:

my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple)  # Output: (1, 2, 3)

When working with imports, always handle exceptions to manage potential errors gracefully:

try:
  my_module = importlib.import_module('some_nonexistent_module')
  except ModuleNotFoundError as e:
  print(f"Error importing module: {e}")

By addressing these points, you should be able to resolve both your TypeError and installation issues effectively. If you still have further questions or need additional clarification on any point, feel free to ask!

Thank you for your messages, they've been very helpful!
I am starting to understand the matlab-python relationship a lot better and some of the small tests I run are working perfectly. However, I think I have found the root of the problem:
From the previous message, when I run:
np = py.importlib.import_module('numpy');
I get the following error:
>> np = py.importlib.import_module('numpy');
Error using __init__><module> (line 119)
Python Error: ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
Error in <frozen importlib>_call_with_frames_removed (line 228)
Error in <frozen importlib>exec_module (line 850)
Error in <frozen importlib>_load_unlocked (line 680)
Error in <frozen importlib>_find_and_load_unlocked (line 986)
Error in <frozen importlib>_find_and_load (line 1007)
Error in <frozen importlib>_gcd_import (line 1030)
Error in __init__>import_module (line 127)
A similar error message appears when I try to run the following:
python_wrapper = ["import matplotlib.pyplot as plt"]
pyrun(python_wrapper)
output:
python_wrapper_code =
"import matplotlib.pyplot as plt"
Error using __init__><module> (line 119)
Python Error: ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
Error in cbook><module> (line 24)
Error in __init__><module> (line 159)
Error in <string>><module> (line 1)
and
python_wrapper_code = ["from fooof import FOOOF"]
pyrun(python_wrapper_code)
output:
python_wrapper_code =
"from fooof import FOOOF"
Error using __init__><module> (line 119)
Python Error: ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
Error in fit><module> (line 62)
Error in __init__><module> (line 3)
Error in __init__><module> (line 15)
Error in <string>><module> (line 1)
.including several others such as:
  • import numpy as np
  • from fooof.sim.gen import gen_aperiodic
In conclusion, all of these packages and more are necessary to run 'fooof'. Even if i change the numpy.array line of code to convert a .mat file, numpy is still essential in too many parts of the package which is what I was afraid of.
I tried to uninstall and reinstall python on my laptop - which has all been done successfully!
I created a new environemnt that is compatible with MATLAB2021b and the issue still remains.
Is it possible that I need to set a path to python/numpy/pip somewhere on my home profile or in the new environment? I can't think of another way to fix the "error importing numpy".
Again thank you very much for your advice!

Hi @Vyte Jan,

The error message you are receiving indicates that you are trying to import numpy from its source directory. This typically occurs when the current working directory is set to the location where numpy is installed as source code, rather than where it is installed as a package. Ensure that your current working directory in MATLAB is not pointing to the numpy source directory. You can check this by using:

   pwd

If it shows a path within the numpy source folder, change it to a different directory where you usually run your scripts.

Setting Up the Correct Python Environment

Using pyenv: To specify which Python environment MATLAB should use, execute:

     pyenv('Version', 'path_to_your_python_executable');

Make sure that this path points to a valid installation of Python that has numpy properly installed. Please click the following link for more information regarding Python compatibility.

https://www.mathworks.com/support/requirements/python-compatibility.html

Creating a Virtual Environment: If you haven’t already, consider creating a virtual environment specifically for your MATLAB project. Here’s how you can do this

     python -m venv myenv
     source myenv/bin/activate  # On Windows use: myenv\Scripts\activate
     pip install numpy matplotlib fooof

After activating this environment, ensure MATLAB is using it with the pyenv command.

Verifying Your Installation: Open a terminal or command prompt and activate your virtual environment. Run Python and check if you can import numpywithout issues:

     import numpy as np
     print(np.__version__)

If this works fine outside of MATLAB, then your Python installation is correct.

Environment Variables: Check if the PYTHONHOME and PATH environment variables are correctly set. In MATLAB, you can check these with:

   getenv('PYTHONHOME')
   getenv('PATH')

If these variables are pointing to incorrect locations (like a previous installation), it could lead to conflicts.

Restarting MATLAB: After making changes, ensure you restart MATLAB to apply any changes made with pyenv.

Hope this helps.

Please let me know if you have any further questions.

Thank you for your messages!
Apologies for the long reply,
From now on I will check pwd everytime I run MATLAB - the directory is usually set to desktop or a fodler on my desktop where the scripts are located.
I have been creating & running the code in virtual env. The currect venv im using is fooof_venv. The path to this env is: /Users/c1034944/ANALYSIS/fooof_venv
and after running 'pip install numpy matplotlib fooof' (on terminal, whilst the venv is activated), I get a long list of 'Requirment satisfied'. Also, numpy has been installed. If I run: 'import numpy as np' and 'print(np.__version__)' this is the output:
/Users/c1034944/ANALYSIS/fooof_venv/bin/python /Users/c1034944/ANALYSIS/fooof_venv/quick_test.py
2.0.2
Process finished with exit code 0
Up to this point I think everything is installed and set up well.
I believe the issue is with PYTHONPATH, PYTHONHOME and PATH. I tried setting these in ~/.bash_profile and almost broke my path.
I run the following in a newly opened matlab window and there are following matlab 'outputs':
pwd
output:
'/Users/c1034944/Desktop'
pyenv
output:
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/Users/c1034944/ANALYSIS/fooof_venv/bin/python"
Library: "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/libpython3.9.dylib"
Home: "/Users/c1034944/ANALYSIS/fooof_venv"
Status: NotLoaded
ExecutionMode: InProcess
python_env_path ='/Users/c1034944/ANALYSIS/fooof_venv/bin/python'
pyenv("Version",python_env_path)
pyenv
output:
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/Users/c1034944/ANALYSIS/fooof_venv/bin/python"
Library: "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/libpython3.9.dylib"
Home: "/Users/c1034944/ANALYSIS/fooof_venv"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "19181"
ProcessName: "MATLAB"
Now checking the paths:
>> getenv('PYTHONHOME')
ans =
0×0 empty char array
>> getenv('PATH')
ans =
'/usr/bin:/bin:/usr/sbin:/sbin'
>> getenv('PYTHONPATH')
ans =
0×0 empty char array
Python virtual env is loaded in and it works (ie I can run py.list), but matlab is not finding the paths. And the numpy error remains. This is being run on MATLAB 2021b and Python 3.9 (which are compatible)
Alternatively, if I start MATLAB through terminal:
>> getenv('PYTHONHOME')
ans =
'/Library/Frameworks/Python.framework/Versions/3.12/bin/python3'
>> getenv('PATH')
ans =
'/usr/local/opt/python/libexec/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/c1034944/fsl/share/fsl/bin:/Library/Frameworks/Python.framework/Versions/3.12/bin:/usr/local/opt/python/libexec/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Users/c1034944/.local/bin'
>> getenv('PYTHONPATH')
ans =
'/Library/Frameworks/Python.framework/Versions/3.12/bin/python3'
Matlab can find python paths straight away, but produces a new error - I cant tell matlab to use a virtual environment. This happens when I try to specify which Python environment MATLAB should use:
python_env_path ='/Users/c1034944/ANALYSIS/fooof_venv/bin/python'
pyenv("Version",python_env_path)
output:
Error using pyenv
Path argument does not specify a valid executable.
I'm guessing the PATHS in ~/.bash_profile are incorrect?
I feel really bad bothering you with all these questions. Please tell me if this unsolvable, I don't want to take up more of your time!
Vyte
Hi @ Vyte Jan,
Hope you are not in rush. Let me analyze your comments and provide solutions to your problem. Also, you are not taking up my time, I am here to help.

Hi @Vyte Jan ,

First, let me make that your Python environment is correctly set up and recognized by MATLAB. Make sure that the version of Python in your virtual environment (fooof_venv,Python 3.9) is compatible with MATLAB 2021b. According to MathWorks documentation, Python 3.9 is indeed supported. Also, specify the path to your virtual environment's Python executable directly in MATLAB using:

    python_env_path = '/Users/c1034944/ANALYSIS/fooof_venv/bin/python';
    pyenv('Version', python_env_path);

After executing this, verify if the pyenv command confirms that the environment has been loaded correctly.

Your current output for getenv('PYTHONHOME), getenv(PATH, and getenv(PYTHONPATH) indicates that they are either empty or not pointing to the correct locations when running MATLAB from the terminal.

PYTHONHOME: This variable should not typically be set for virtual environments; it might cause conflicts. It is best to leave it unset.

PYTHONPATH: If you have custom modules that need to be recognized by Python, you can set this variable; however, it should include paths where your modules are located, not the path to the Python executable.

PATH: Make sure that your PATH variable includes the directory of your virtual environment’s binaries. Here’s how you can modify your .bash_profile.

      # Add virtual environment to PATH
      export PATH="/Users/c1034944/ANALYSIS/fooof_venv/bin:$PATH"

After making changes, remember to source your .bash_profileor restart your terminal:

   source ~/.bash_profile

Starting MATLAB Correctly

When starting MATLAB from the terminal, ensure you're doing so after activating your virtual environment:

source /Users/c1034944/ANALYSIS/fooof_venv/bin/activate
matlab

This will ensure that MATLAB inherits the activated environment's settings.

Resolving Import Errors

If you continue facing issues with importing NumPy in MATLAB:Make sure you can run a simple script in Python outside of MATLAB to confirm that NumPy is functioning properly.Use:

    import numpy as np
    print(np.__version__)

If this works without issues, but fails in MATLAB, try running a simple command directly in MATLAB:

py.importlib.import_module('numpy');

By following these steps systematically, you should be able to resolve the issues related to configuring and using your Python virtual environment within MATLAB effectively.

Thank you for your time and messages. Apologies for the late reply - I typed up a reply and forgot to post it!
I followed the recommended steps as closely as possible:
Firstly, when I run the following commands from a fresh matlab window:
% specify the path to your virtual environment's Python executable directly in MATLAB using:
python_env_path = '/Users/c1034944/ANALYSIS/fooof_venv/bin/python';
pyenv('Version', python_env_path);
% verify if the pyenv command confirms that the environment has been loaded correctly.
pyenv
...pyenv confirms that python is not loaded:
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/Users/c1034944/ANALYSIS/fooof_venv/bin/python"
Library: "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/libpython3.9.dylib"
Home: "/Users/c1034944/ANALYSIS/fooof_venv"
Status: NotLoaded
ExecutionMode: InProcess
So at this point I would run something like: py.list({'This','is a','list'}), and I get a python output in the matlab command window. Once I run pyenv again, PythonEnvironment changes to:
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/Users/c1034944/ANALYSIS/fooof_venv/bin/python"
Library: "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/libpython3.9.dylib"
Home: "/Users/c1034944/ANALYSIS/fooof_venv"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "34029"
ProcessName: "MATLAB"
I don't think this is a problem, just curious that it doesn't load in without me having to run a python command.
Thank you for explaning the PATHS! From what I've read online, PYTHONPATH is usally set to very specific installations. I don't have any specific modules so I've completely removed PYTHONPATH from my bash_profile. I have updated my bash_profile using: export PATH="/Users/c1034944/ANALYSIS/fooof_venv/bin:$PATH". Right now the only things in my bash profile are PATH to fooof_venv and FSL set-up
I also added a symlink to my bash profile to be able to start matlab using $ matlab as described here: https://uk.mathworks.com/matlabcentral/answers/523535-adding-matlab-to-my-path-mac
When I start matlab through terminal (from the fooof_venv), the getenv('PYTHONHOME') and getenv('PYTHONPATH') are empty: (Python Environment is loaded here)
ans =
0×0 empty char array
Also, running a simple numpy through python with fooof_venv activated:
My code:
# Quick check on numpy from python directly
import numpy as np
print(np.__version__)
output:
/Users/c1034944/ANALYSIS/fooof_venv/bin/python /Users/c1034944/ANALYSIS/fooof_venv/quick_test.py
2.0.2
Process finished with exit code 0
The python venv is set up well and numpy is definetly installed and working. I think the PATHS are still not pointing to the right locations. When I run $ echo PATH I get the following output:
/Users/c1034944/fsl/share/fsl/bin:/Users/c1034944/fsl/share/fsl/bin:/Users/c1034944/ANALYSIS/fooof_venv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/System/Cryptexes/App/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Users/c1034944/.local/bin
I am not sure what else could be the problem. When I googled the numpy issue I found this: https://github.com/numpy/numpy/issues/24965 - suggesting that the problem might be a lot deeper.
I actually got the fooof code to run on a windows pc and i'm hoping i will be able to do my analysis from there. Thank you so much for your time and advice, its unfortunate we couldn't get the code to work on mac.

Hi @Vyte Jan,

Integrating Python with MATLAB can sometimes be a nuanced process, especially when dealing with virtual environments. Let's break down the situation and address your concerns step by step.

Understanding the pyenv Command

The pyenv command in MATLAB is crucial for managing the Python environment. When you first set the Python executable using:

python_env_path = '/Users/c1034944/ANALYSIS/fooof_venv/bin/python';
pyenv('Version', python_env_path);

You are instructing MATLAB to use the specified Python interpreter. However, the initial output indicates that the environment is not loaded:

ans = 
PythonEnvironment with properties:
        Version: "3.9"
     Executable: "/Users/c1034944/ANALYSIS/fooof_venv/bin/python"
        Library: 
        "/Library/Developer/CommandLineTools/Library/Frameworks/
        Python3.framework/Versions/3.9/lib/libpython3.9.dylib"
           Home: "/Users/c1034944/ANALYSIS/fooof_venv"
         Status: NotLoaded

This means that while you have specified the Python executable, MATLAB has not yet initialized the environment.

Why Must You Run a Python Command?

The reason you need to execute a Python command (like py.list({'This','is a','list'})) to load the environment is that MATLAB only initializes the Python environment upon the first call to a Python function. This behavior is by design, as it allows MATLAB to ensure that the Python environment is correctly set up before executing any commands. After running a Python command, the environment status changes to "Loaded," confirming that MATLAB has successfully initialized the Python interpreter.

Environment Variables: PYTHONHOME and PYTHONPATH

You mentioned that both getenv('PYTHONHOME') and getenv('PYTHONPATH') return empty values. This is expected behavior when using a virtual environment, as these variables are typically set to point to specific installations of Python and its libraries. In a virtual environment, the paths are managed internally, and you generally do not need to set these variables manually.

  • PYTHONHOME: This variable is used to specify the location of the standard Python libraries. In a virtual environment, it is usually not set, as the environment itself contains the necessary libraries.
  • PYTHONPATH: This variable is used to specify additional directories that Python should add to the module search path. Since you have removed it from your bash_profile, it should not interfere with your virtual environment.

PATH Configuration

Your PATH variable appears to be correctly set up to include the virtual environment's bin directory:

export PATH="/Users/c1034944/ANALYSIS/fooof_venv/bin:$PATH"

This ensures that when you run MATLAB from the terminal, it can access the Python executable within your virtual environment.

Testing NumPy Functionality

You have successfully tested NumPy outside of MATLAB, confirming that it is installed and functioning correctly:

import numpy as np
print(np.__version__)

The output indicates that NumPy is indeed available in your virtual environment. This is a positive sign that the environment is set up correctly.

My recommendations are listed below.

Loading the Environment: Continue to use a Python command to load the environment in MATLAB. This is standard behavior and not indicative of a problem.

Environment Variables: Do not worry about PYTHONHOME and PYTHONPATH being empty; this is typical for virtual environments.

MATLAB Execution: Ensure that you always start MATLAB from the terminal where the virtual environment is activated to maintain the correct context.

If you continue to experience issues, consider checking for any specific compatibility problems between MATLAB and the version of Python or NumPy you are using. Additionally, ensure that your MATLAB version supports the Python version you are working with.

If you have further questions or need additional assistance, feel free to ask. Your setup seems well on its way to functioning correctly, and with a few adjustments, you should be able to run your analyses seamlessly.

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

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.

Asked:

on 27 Sep 2024

Commented:

on 15 Oct 2024

Community Treasure Hunt

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

Start Hunting!