import numpy to matlab

244 次查看(过去 30 天)
Ronald Kurnik
Ronald Kurnik 2018-7-15
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