Calling python modules gives error

3 vues (au cours des 30 derniers jours)
Richard
Richard le 5 Déc 2014
I have a custom python module and am trying to call a function from it in matlab (2014b). It takes a scalar (integer but there are no checks in the module) as input. Using the documentation from http://www.mathworks.com/help/matlab/matlab_external/call-python-from-matlab.html
hasn't helped. I have created the mymod.py there and can execute all of the commands from that demo. My module Bidiff.py is in the same folder as mymod.py (which has always been my pwd in the session). However, if I try something like
A=py.Bidiff.GenerateNoise(30)
in matlab, I get the error
Undefined variable "py" or function "py.BiDiff.GenerateNoise".
this takes quite a while (2-3 minutes) before it returns the error. The code runs in python; I can import BiDiff into a python session and run
Bidiff.GenerateNoise(30)
and receive the output in under 3 seconds. So the python module is working.
pyversion returns my current installation (2.7) and shows isloaded: 1 (I assume since I ran the demo). If it matters, I am on a Linux machine for this (trying to explain why http://www.mathworks.com/help/matlab/matlab_external/undefined-variable-py-or-function-py-command.html hasn't helped).

Réponses (3)

Robert Snoeberger
Robert Snoeberger le 5 Déc 2014
Hi Richard,
For the information you have given, there are two possible issues 1) module BiDiff is not imported or 2) function GenerateNoise is not available. Can you access any other functions in your custom python module BiDiff.py?

Richard
Richard le 5 Déc 2014
Hi Robert,
I tried running the other function in the module that I CAN run. It also runs fine in a python prompt after importing the module. However this time, the error from matlab was (I have to pass some python tuples a and b which are generated fine by py.tuple):
A=py.BiDiff.Interfacer(a,b,N)
Undefined variable "py" or class "py.BiDiff.Interfacer"
I would note that Interfacer is a function just like the GenerateNoise, not a class.

Robert Snoeberger
Robert Snoeberger le 5 Déc 2014
Richard, please try the following and let me know if there are any errors:
BiDiff = py.eval('__import__(''BiDiff'')', struct);
func = BiDiff.GenerateNoise;
A = feval(func, int32(30))
When you are working in the python prompt, what is the line you use to import your module?
  5 commentaires
Kevin O'Connor
Kevin O'Connor le 7 Juil 2016
I am having the same problem that Richard was having. I did what you asked him to do and when I do that I get a barrage of errors that I do not understand. They are as follows:
>> connectM = py.eval('__import__(''connectM'')', struct);
func = connectM.connectM;
A = feval(func, int32(30))
Error using enginesession>__init__ (line 8)
Python Error: EngineError: MATLAB process session cannot be initialized.
Error in __init__><module> (line 80)
_session = EngineSession()
Error in connectM><module> (line 19)
import matlab.engine
Error in <string>><module> (line 1)
And so I do not understand what is going on. My pyversion all seems valid, and when I use
from connectM import connectM
eng = connectM()
in the python interpreter everything is fine. Nowhere else seems to have any more information on this particular set of errors.
Robert Snoeberger
Robert Snoeberger le 8 Juil 2016
Kevin,
Richard's problem was due to an incompatible version of libQVTK.so being used when calling Python from MATLAB. Your problem is different. Based on the error message "EngineError: MATLAB process session cannot be initialized," you are trying to use matlab.engine within MATLAB. You should post a new question.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Call MATLAB from Python dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by