python method from matlab object

5 views (last 30 days)
Sylvain
Sylvain on 25 Apr 2025
Edited: Adarsh on 2 May 2025
Here is the problem I have,
I have a python class, which contain properties that are memebers of specific class, typically to rotate a motor.
The class is defined as below, it uses the pytrinamic for the PD42-1370 motors: https://github.com/analogdevicesinc/PyTrinamic
I have made a wrapper class of the example provided for the Pytrinamic: https://github.com/analogdevicesinc/PyTrinamic/blob/master/examples/modules/TMCM1370/TMCL/rotate_demo.py
"""
pd42_1370.py wrapper to connect to the PD42-X-1370 stepper motor
"""
import pytrinamic
from pytrinamic.connections import ConnectionManager
from pytrinamic.connections import UsbTmclInterface
from pytrinamic.modules import TMCM1370
class PD42_1370():
def __init__(self,com_port,baudrate):
self.com_port = com_port
self.baudrate = baudrate
options = "--interface serial_tmcl --port "+ str(self.com_port)+ " --data_rate "+str(self.baudrate)
self.connection_manager = ConnectionManager(options)
self.interface = self.connection_manager.connect()
print("connected")
self.module = TMCM1370(self.interface)
print("module added")
self.motor = self.module.motors[0]
def disconnect(self):
self.connection_manager.disconnect()
print("disconnected")
def rotateRight(self,speed):
self.motor.rotate(speed)
my MATLAB code is:
py.importlib.import_module('pd42_1370')
ax = py.pd42_1370.PD42_1370('COM5',9600)
ax.rotateRight(int32(10000000))
ax.disconnect()
It is working really fine. But instead of sending the command:
ax.rotateRight(int32(10000000))
I would like to write it:
ax.motor.rotate(int32(100000000))
this later throw back an error:
Unrecognized method, property, or field 'rotate' for class 'py.pytrinamic.modules.TMCM1370._MotorTypeA'.
I cannot figure out what is wrong.
If this could be sorted, then it would help by using direct calls to python, rather than keeping developping the wrapper.
  1 Comment
Sylvain
Sylvain on 25 Apr 2025
I am digging a bit and I was able to look at some MATLAB coding without creating any additional class, but still the same issue.
In addition, I see that Matlab is not able to call modules that have a "_" front of their name.
>> py.pytrinamic.modules.TMCM1370._MotorTypeA
py.pytrinamic.modules.TMCM1370._MotorTypeA
Error: Invalid text character. Check for unsupported symbol, invisible character,
or pasting of non-ASCII characters.

Sign in to comment.

Answers (1)

Adarsh
Adarsh on 28 Apr 2025
I understand that you are facing an error in directly modifying an attribute of a Class in an imported python module.
There are some limitations to the Python Interface for MATLAB, here are a few of them that are related to this error:
  1. Customized (dynamic) attribute access.
  2. Dynamically generated Python classes.
  3. Class names or other identifiers starting with an underscore (_) character. Instead, use the Python “py.getattr” and “py.setattr” functions.
To resolve this issue, you can try the following workarounds:
  1. Execute the code in Python Out-Of-Processs Execution Mode – Here python runs in a separate process other than MATLAB, supporting python libraries that are not supported by MATLAB.
  2. If the problem is with the class of motor beginning with an “_” then you can try using the “py.getattr”, “py.setattr”to modify the attributes.
This issue can occur if the python module which is being used is not supported in MATLAB, even in this scenario you can try executing the code in Out-Of-Process mode to ensure the error is not related to MATLAB.
For more details, you can refer to the following documentation links:
  1. https://www.mathworks.com/help/releases/R2024b/matlab/matlab_external/limitations-to-python-support.html
  2. https://www.mathworks.com/help/releases/R2024b/matlab/matlab_external/out-of-process-execution-of-python-functionality.html
I hope it helps in resolving the issue.
  2 Comments
Sylvain
Sylvain on 30 Apr 2025
Thanks, I am already executing the code in Out-Of-Process, so I guess the error comes from that "_" in the class name.
I am not really sure how to use the py.getattr and py.setattr syntax. Do you have any example somewhere?
Adarsh
Adarsh on 2 May 2025
Edited: Adarsh on 2 May 2025
"getattr" and "setattr" are part of Python's core functionality.
"getattr" - retrieves the value of attribute from object
Example:
val = py.getattr(pyObj, 'foo'); % This retrieves the value of 'foo' attribute in 'pyObj' object instance.
"setattr" - Sets an attribute of an object to a value.
Example:
py.setattr(pyObj, 'foo', 123); % This sets the value of 'foo' attribute in 'pyObj' object instance to a value.

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!