Check edit text box validity for "i" and "j" characters
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Walter Roberson
2015-10-5
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
2015-10-5
编辑:Stephen23
2015-10-5
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!