Check edit text box validity for "i" and "j" characters
2 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
Walter Roberson
on 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
on 5 Oct 2015
Edited: Stephen23
on 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 -?
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!