Can't we call python operators directly within MATLAB?

조회 수: 6 (최근 30일)
Rafael S.T. Vieira
Rafael S.T. Vieira 2020년 7월 18일
답변: Rajani Mishra 2020년 7월 28일
Hi,
Some python operators are already implemented in MATLAB, however I am unable to call them directly. Is it not possible? For instance:
a = py.math.factorial(100);
a.__add__(a);
returns:
a.__add__(a)
Error: Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII
characters.
Calling __add__ within python is perfectly valid. If I try to execute a + a, MATLAB correctly returns a py.long (in this case, MATLAB calls the python operator as expected), however when I execute:
a / py.math.factorial(50)
It returns a double 3.068518756254966e+93 instead of a py.long. It seems that MATLAB is calling mrdivide instead of __truediv__. That's why I would like to call the operators directly. Am I missing something? Is it possible to configure MATLAB for reading the character underscore _?
PS: The current workaround is to use numpy:
py.numpy.divide(a, py.math.factorial(50))
which correctly returns:
Python long with properties:
denominator: [1×1 py.long]
imag: [1×1 py.long]
numerator: [1×1 py.long]
real: [1×1 py.long]
3068518756254966037202730459529469739228459721684688959447786986982158958772355072000000000000

채택된 답변

Rajani Mishra
Rajani Mishra 2020년 7월 28일
For Python method "_add_" the corresponding MATLAB method is "plus" or "+", as mentioned in the link provided by you.
Below code will not work, as correspondong MATLAB method is to be used.
a.__add__(a)
Also when passing data to Python, MATLAB converts the data into types that best represent the data to the Python language, similarly converts when it receives data from Python. You can look into this link : https://www.mathworks.com/help/matlab/matlab_external/handling-data-returned-from-python.html
bool, int, float, complex types of Python gets converted to MATLAB types, remaining types are given as py.type.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by