Menu

#2 Python 3

Python3
open
nobody
None
2014-08-17
2014-04-17
magro
No

Hello again,

here the issue about my try to run PyMatlab with Python 3. The main problem is, that c_char_p in matlab.py does not accept values of type str but byte (e.g. b"myString"). In my test implementation I simply replaced the values of type str by their byte representation or converted them to byte. This worked. However, I don't now how to implement this, such that its compatible with Python 2 and transformed to valid Python 3 code using py2to3 within the setup-process (this is automatically done when installing a python package under python 3).

One example for the usage are the first lines in the run-method:

def run(self,matlab_statement):
        #wrap statement to be able to catch errors
        real_statement = wrap_script.format(matlab_statement)
        if type(real_statement)==str:
            real_statement = str.encode(real_statement)
        self.engine.engEvalString(self.ep,c_char_p(real_statement))

or

def putvalue(self,name,pyvariable):
        if type(pyvariable)==bytes:
            self.mx.mxCreateString.restype=POINTER(mxArray)
            mx = self.mx.mxCreateString(c_char_p(pyvariable))
        if type(pyvariable)==str:
            self.mx.mxCreateString.restype=POINTER(mxArray)
            mx = self.mx.mxCreateString(c_char_p(str.encode(pyvariable)))

After having changed this, it worked. This is not much work to do, and then this great package can be used with Python 3 ;)

Cheers
Marc

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.