Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
liulion opened this issue Oct 20, 2015 · 5 comments
Open

Comments

@liulion
Copy link

liulion commented Oct 20, 2015

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
Copy link
Author

liulion commented Oct 20, 2015

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
Copy link
Author

liulion commented Oct 20, 2015

PS: my environment is python2.7+matlab2013b

@wangxiongsjtu
Copy link

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

@UpCoder
Copy link

UpCoder commented Mar 23, 2017

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants