Skip to content

Calling from MATLAB

Peter Corke edited this page Mar 4, 2023 · 2 revisions

Just some rough initial notes

THIS IS OUT OF DATE. THERE ARE ISSUES WITH RUNNING PYTHON FROM CONDA ENVIRONMENTS THAT I DON'T HAVE THE MOTIVATION TO FIGURE OUT RIGHT NOW.

Check your Python environment is at least Python 3

>> pyenv
ans = 
  PythonEnvironment with properties:

          Version: "3.7"
       Executable: "/opt/anaconda3/bin/python"
          Library: "/opt/anaconda3/lib/libpython3.7m.dylib"
             Home: "/opt/anaconda3"
           Status: NotLoaded
    ExecutionMode: InProcess

If you want to use a specific Python interpreter, for instance from a conda environment, use a command like this

>> pyenv(Version="~/opt/miniconda3/envs/dev/bin/python")

Check that the packages are in your Python path

>> P = py.sys.path
P = 

  Python list with no properties.

    ['', '/opt/anaconda3/lib/python37.zip', '/opt/anaconda3/lib/python3.7', '/opt/anaconda3/lib/python3.7/lib-dynload', '/opt/anaconda3/lib/python3.7/site-packages']

If spatial math is not there, add it

>> insert(P,int32(0),'~/code/spatialmath-python')  # adjust for your case
>> P = py.sys.path
P = 

  Python list with no properties.

    ['/Users/corkep/code/spatialmath-python', '', '/opt/anaconda3/lib/python37.zip', '/opt/anaconda3/lib/python3.7', '/opt/anaconda3/lib/python3.7/lib-dynload', '/opt/anaconda3/lib/python3.7/site-packages', '/Users/corkep/code/bdsim', '/Users/corkep/code/robotics-toolbox-python', '/Users/corkep/code/machinevision-toolbox-python', '/Users/corkep/code/ropy']

Now import the package

>> sm = py.importlib.import_module('spatialmath')

sm = 

  Python module with properties:

               arg: [1×1 py.module]
                np: [1×1 py.module]
           Plucker: [1×1 py.abc.ABCMeta]
            pose2d: [1×1 py.module]
        quaternion: [1×1 py.module]
             Twist: [1×1 py.abc.ABCMeta]
                tr: [1×1 py.module]
          UserList: [1×1 py.abc.ABCMeta]
               p3d: [1×1 py.module]
             Plane: [1×1 py.type]
              base: [1×1 py.module]
        super_pose: [1×1 py.module]
              math: [1×1 py.module]
    UnitQuaternion: [1×1 py.abc.ABCMeta]
               SE2: [1×1 py.abc.ABCMeta]
        namedtuple: [1×1 py.function]
              quat: [1×1 py.module]
                p3: [1×1 py.module]
             sympy: [1×1 py.module]
               SO3: [1×1 py.abc.ABCMeta]
            SMPose: [1×1 py.abc.ABCMeta]
               plt: [1×1 py.module]
            geom3d: [1×1 py.module]
            pose3d: [1×1 py.module]
              copy: [1×1 py.module]
                sp: [1×1 py.module]
            Twist2: [1×1 py.abc.ABCMeta]
          argcheck: [1×1 py.module]
        Quaternion: [1×1 py.abc.ABCMeta]
                sm: [1×1 py.module]
               SE3: [1×1 py.abc.ABCMeta]
            Axes3D: [1×1 py.type]
               SO2: [1×1 py.abc.ABCMeta]
           SMTwist: [1×1 py.abc.ABCMeta]

    <module 'spatialmath' from '/Users/corkep/code/spatialmath-python/spatialmath/__init__.py'>

Then we are good to use it

>> a=sm.SE3()
>> a
>> a.A

ans = 

  Python ndarray:

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

    Use details function to view the properties of the Python object.

    Use double function to convert to a MATLAB array.

Not sure why it prints nothing. Doesn't seem to use the __repr__ method, I added a display method, which does the same thing for MATLAB objects, but it causes no display as shown above. Without display we see the properties and the value.

A property of the Python object is the translation vector:

>> a.t

ans = 

  Python ndarray with properties:

           T: [1×1 py.numpy.ndarray]
        base: [1×1 py.numpy.ndarray]
      ctypes: [1×1 py.numpy.core._internal._ctypes]
        data: [1×3 py.memoryview]
       dtype: [1×1 py.numpy.dtype]
       flags: [1×1 py.numpy.flagsobj]
        flat: [1×1 py.numpy.flatiter]
        imag: [1×1 py.numpy.ndarray]
    itemsize: [1×1 py.int]
      nbytes: [1×1 py.int]
        ndim: [1×1 py.int]
        real: [1×1 py.numpy.ndarray]
       shape: [1×1 py.tuple]
        size: [1×1 py.int]
     strides: [1×1 py.tuple]

    [0. 0. 0.]

Not sure why it prints all the properties...

Clone this wiki locally