Why isnumeric function on a Python String returns different result from MATLAB documentation
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MathWorks Support Team
il 22 Giu 2017
Modificato: MathWorks Support Team
il 26 Apr 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
Risposta accettata
MathWorks Support Team
il 26 Apr 2023
Modificato: MathWorks Support Team
il 26 Apr 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Call Python from MATLAB in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!