import numpy to matlab

조회 수: 365 (최근 30일)
Ronald Kurnik
Ronald Kurnik 2018년 7월 15일
답변: Paul Kuberry 2020년 3월 26일
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
Stephen23
Stephen23 2018년 7월 15일
"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 2018년 7월 15일
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)

댓글을 달려면 로그인하십시오.

답변 (3개)

Tushar Agarwal
Tushar Agarwal 2019년 9월 26일
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 2019년 7월 12일
I have same error. Can someone please help on this ?

Paul Kuberry
Paul Kuberry 2020년 3월 26일
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.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by