Effacer les filtres
Effacer les filtres

Check edit text box validity for "i" and "j" characters

2 vues (au cours des 30 derniers jours)
Matt
Matt le 5 Oct 2015
Modifié(e) : Stephen23 le 5 Oct 2015
Hello,
I have in my GUI an edit text item where the user has to enter only numbers. I check this with the following command:
isnan(str2double(get(hObject, 'String')))
However, when you input i or j, this condition is not satisfied. What is the trick to solve this issue?

Réponses (2)

Walter Roberson
Walter Roberson le 5 Oct 2015
There is nothing to solve. i and j are number-forming characters in MATLAB, just like d and e and '+' and '-' and '.' are.
If you wanted to test for only digits rather than for number then you would use a different test. For example, you could use ismember()

Stephen23
Stephen23 le 5 Oct 2015
Modifié(e) : Stephen23 le 5 Oct 2015
Rather than converting to numeric, simply check that the string contains only digits. This is easy using the inbuilt function isstrprop:
>> all(isstrprop('123', 'digit'))
ans = 1
>> all(isstrprop('123i', 'digit'))
ans = 0
Note that you need to decide what constitutes a "number", as i and j could be considered to be numbers, just like e or π. What about + and -?

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by