arokem / python-matlab-bridge

A simple Python => MATLAB(R) interface and a matlab_magic for ipython
BSD 2-Clause "Simplified" License
335 stars 96 forks source link

"UnicodeDecodeError: 'utf8' codec can't decode error when calling matlab #227

Open liulion opened 8 years ago

liulion commented 8 years ago

1、【matlabCode】 function ret=testFun a=20;
b=10; ret=a+b;

2、【Python Code】 matlab=pymat.Matlab() matlab.start() func_file=u"G:/Matlab/MyMatlab/testFun.m".decode("utf-8") res2=matlab.run_func(func_file)

3、【The errors】 Starting MATLAB on ZMQ socket tcp://127.0.0.1:53224 Send 'exit' command to kill the server .....MATLAB started and connected! Traceback (most recent call last): File "G:/Python/quanGeek/model/test.py", line 88, in res2=matlab.run_func(func_file) File "D:\Anaconda\lib\site-packages\pymatbridge-0.6.dev0-py2.7.egg\pymatbridge\pymatbridge.py", line 311, in run_func nargout=nargout) File "D:\Anaconda\lib\site-packages\pymatbridge-0.6.dev0-py2.7.egg\pymatbridge\pymatbridge.py", line 274, in _json_response return json.loads(self._response(**kwargs), object_hook=decode_pymat) File "D:\Anaconda\lib\site-packages\pymatbridge-0.6.dev0-py2.7.egg\pymatbridge\pymatbridge.py", line 232, in _response resp = self.socket.recv_string() File "D:\Anaconda\lib\site-packages\zmq\sugar\socket.py", line 403, in recv_string return b.decode(encoding) File "D:\Anaconda\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0xca in position 22: invalid continuation byte MATLAB closed

4、Then I debuge the errors , I found the error is caused by the codeing:
def decode(input, errors='strict'): return codecs.utf_8_decode(input, errors, True) in "D:\Anaconda\Lib\encodings\utf_8.py" file

5、the reason is the input parameter of 'input' is '{"content":{"stdout":"�������̫�ࡣ"},"result":"","stack":[],"success"'

it look like that 'stdout' paramter has error code , but how could I resolve it ? the problem has block me for almost 2 weeks, would you help me ?

PS: [the error snapshot ] image

liulion commented 8 years ago

appending more information: It is right when I call the function in Ipython. the snapshot as follows: image

But why it is error when I call matlab in python?

liulion commented 8 years ago

PS: my environment is python2.7+matlab2013b

wangxiongsjtu commented 7 years ago

Hei, do you address this problem. Recently I also have encountered the same problem but do not know why

UpCoder commented 7 years ago

I meet the problem too. I find the error happened when matlab function return a Chinese error information. So I change the language of system from Chinese to English. By the way, the way passing params, is use 'paramname',paramValue not use a json format. such as matlab.run_func('test.m','argm1',argm1Value,'argm2',argm2Value) not matlab.run_func('test.m',{ 'argm1':argm1Value, 'argm2':argm2Value })

happyalfred2016 commented 5 years ago

I meet the problem too. I find the error happened when matlab function return a Chinese error information. So I change the language of system from Chinese to English. By the way, the way passing params, is use 'paramname',paramValue not use a json format. such as matlab.run_func('test.m','argm1',argm1Value,'argm2',argm2Value) not matlab.run_func('test.m',{ 'argm1':argm1Value, 'argm2':argm2Value })

I solve the problem by setting the language of matlab into English. I think the problem only happens when it return the result from matlab to python, and somehow the result contains some special letters/characters from other languages which causes the error mentioned above.