python method from matlab object
Show older comments
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
on 25 Apr 2025
Answers (1)
Adarsh
on 28 Apr 2025
0 votes
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:
- Customized (dynamic) attribute access.
- Dynamically generated Python classes.
- 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:
- 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.
- 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:
- https://www.mathworks.com/help/releases/R2024b/matlab/matlab_external/limitations-to-python-support.html
- 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
on 30 Apr 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.
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!