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

3 Ansichten (letzte 30 Tage)
Matt
Matt am 5 Okt. 2015
Bearbeitet: Stephen23 am 5 Okt. 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?

Antworten (2)

Walter Roberson
Walter Roberson am 5 Okt. 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 am 5 Okt. 2015
Bearbeitet: Stephen23 am 5 Okt. 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 -?

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by