Why isnumeric function on a Python String returns different result from MATLAB documentation
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 22 de Jun. de 2017
Editada: MathWorks Support Team
el 26 de Abr. de 2023
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
Respuesta aceptada
MathWorks Support Team
el 26 de Abr. de 2023
Editada: MathWorks Support Team
el 26 de Abr. de 2023
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Call Python from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!