2

I am following this link to run matlab code in python: Running Matlab using Python gives 'No module named matlab.engine' error

I am getting the error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\XYZ\AppData\Local\Programs\Python\Python36\lib\site-packages\matlab\engine\matlabengine.py", line 71, in __call___stderr, feval=True).result()
File "C:\Users\XYZ\AppData\Local\Programs\Python\Python36\lib\site-packages\matlab\engine\futureresult.py", line 67, in result
return self.__future.result(timeout)
File "C:\Users\XYZ\AppData\Local\Programs\Python\Python36\lib\site-packages\matlab\engine\fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None,out=self._out, err=self._err)

matlab.engine.EngineError: MATLAB function cannot be evaluated

I am using Python 3.6 windows 7, 64 bit Matlab R2018b version

I have just written a simple function in matlab and saved the file as demo.m The code is here:

 printk();
 function printk()
     fprintf(' Hello world!');
 end

and saved the file as demo.mat

Python idle:

import matlab.engine
eng = matlab.engine.start_matlab()
eng.demo(nargout=0)

Can anyone please help. I need to migrate whole bunch of codes in python. Any other possible way would be appreciated. Thanks.

5
  • 1
    Did you read the instructions on how to install the Python package? mathworks.com/help/matlab/matlab_external/… Commented Dec 3, 2018 at 16:45
  • Does it only happen with fprintf? have you tried some other function that does not use MATLAB's I/O features? Perhaps you cannot write in command line using MATLAB fucntions in python Commented Dec 3, 2018 at 16:48
  • Yes I installed in the same way as mentioned in the link At a macOS or Linux operating system prompt — cd "matlabroot/extern/engines/python" python setup.py install Instead I used windows. Commented Dec 3, 2018 at 16:51
  • I need to check if it happens only with fprintf. Is there any alternative for it? Commented Dec 3, 2018 at 16:52
  • you say you saved it as a .mat file? .mat files are for matlab data. You should save it as a .m file. Apart from this I'm not sure if fprintf would work the way you want it to. I have no experience with the python engine but i know that with the c engine the print commands don't do anything. These output to the matlab command window and not to your python command window. If you want to print out something i think you're better off returning them to python and printing them there. Commented Dec 4, 2018 at 14:59

1 Answer 1

0

You need 64 bit python, if you have 64 bit matlab.

Maybe you are looking for something like: https://github.com/Wilhox/Matlab-Shell-for-python.git

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.