Why isnumeric function on a Python String returns different result from MATLAB documentation
조회 수: 5 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2017년 6월 22일
편집: MathWorks Support Team
2023년 4월 26일
Is the following behavior an unexpected result from the function 'isnumeric' on a Python string?
>> x = py.str('1')
x =
Python str with no properties.
1
>> isnumeric(x)
ans =
1
>> a+1
Python Error: Can't convert 'float' object to str implicitly
채택된 답변
MathWorks Support Team
2023년 4월 26일
편집: MathWorks Support Team
2023년 4월 26일
The result of the 'isnumeric' function on a Python string is true because the Python version of 'isnumeric' has been executed instead of MATLAB 'isnumeric' function.
In MATLAB, 'isnumeric' is used to determine if a variable is a numeric array:
>> help isnumeric
... isnumeric(A) returns true if A is a numeric array and
false otherwise.
For example, integer and float (single and double) arrays are considered numeric, while logicals, strings, cell arrays, and structure arrays are not.
The Python string class in Python 3.X also has a method called 'isnumeric'. It will return true if a string contains only numeric characters. Below is a Python 3.3 example to be executed in a Python IDE:
>>> s = str('1')
>>> s.isnumeric()
True
>>>
>>> help(str.isnumeric)
... Return True if there are only numeric characters in S,
False otherwise.
Thus if a variable 'x' is of class 'py.str', then calling 'x.isnumeric' and 'isnumeric(x)' are equivalent and will execute the Python version of 'isnumeric'.
When working with Python variables in MATLAB, a method or function name can appear in both languages but have slightly different meanings. It is important to note that MATLAB will look for a method before looking for a function, according to function precedence order. For more details about function precedence order, please refer to the link below:
https://www.mathworks.com/help/matlab/matlab_prog/function-precedence-order.html
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!