import numpy to matlab

244 views (last 30 days)
Ronald Kurnik
Ronald Kurnik on 15 Jul 2018
Answered: Paul Kuberry on 26 Mar 2020
I have installed python 3.6 as well as spyder, numpy, pandas, scipy, and matplotlib from the Anaconda3(64 bit) distribution. I followed the MATLAB directions and call call simple python scrips in MATLAB. What I have not been able to do is to use any of the packages numpy, pandas, scipy, and matplotlib. The MATLAB command py.importlib.import_module('numpy') does not work.
Thanks in advance for your help.
  2 Comments
Stephen23
Stephen23 on 15 Jul 2018
"The MATLAB command py.importlib.import_module('numpy') does not work."
Please explain exactly what happens when you try it, including the complete error messages or warnings if you get any.
Stephen23
Stephen23 on 15 Jul 2018
Ronald Kurnik's "Answer" moved here:
This is my code saved as d6.py
py.importlib.import_module('numpy')
def fun2(x,z1,z2,z3,z4,z5):
y=z1+z2*x + z3/((1+numpy.exp(-z4*(x-z5))))
return y
pyversion
version: '3.6'
executable: 'C:\Users\Ron\Anaconda3\pkgs\python-3.6.5-h0c2934d_0\python.exe'
library: 'C:\Users\Ron\Anaconda3\pkgs\python-3.6.5-h0c2934d_0\python36.dll'
home: 'C:\Users\Ron\Anaconda3\pkgs\python-3.6.5-h0c2934d_0'
isloaded: 0
>> py.math.exp(1)
ans =
2.7183
>> d6 = py.importlib.import_module('d6');
Error using d6><module> (line 2)
Python Error: NameError: name 'py' is not defined
Error in <frozen importlib>_call_with_frames_removed (line 219)
Error in <frozen importlib>exec_module (line 678)
Error in <frozen importlib>_load_unlocked (line 665)
Error in <frozen importlib>_find_and_load_unlocked (line 955)
Error in <frozen importlib>_find_and_load (line 971)
Error in <frozen importlib>_gcd_import (line 994)
Error in __init__>import_module (line 126)
return _bootstrap._gcd_import(name[level:], package, level)

Sign in to comment.

Answers (3)

Tushar Agarwal
Tushar Agarwal on 26 Sep 2019
Although Anaconda with MATLAB is not officially supported, you could access it using anaconda prompt. Please refer to this question https://www.mathworks.com/matlabcentral/answers/427187-problem-with-python-numpy for more details.
I am summarizing the straightforward approach that worked for me:
  1. Open Anaconda prompt (comes with the package). Optional: Activate any environments if you wish or just work with the default base environment.
  2. Change the directory to the one containing the matlab executable. Eg: "cd C:\Program Files\MATLAB\R2018b\bin"
  3. Run matlab by just running "matlab" command in the prompt.
  4. Verify using something like "py.help('numpy')" in the loaded matlab window.
Please Up-vote if it helps you. Thanks!

Saurabh Kelkar
Saurabh Kelkar on 12 Jul 2019
I have same error. Can someone please help on this ?

Paul Kuberry
Paul Kuberry on 26 Mar 2020
mkl_* libraries are not in a location where they can be loaded when calling Python from Matlab (unless Matlab is called using the Anaconda Prompt). This is why the issue appears for any Python package that relies on Numpy.

Products

Community Treasure Hunt

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

Start Hunting!