Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
Matt
Matt el 5 de Oct. de 2015
Editada: Stephen23 el 5 de Oct. de 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?

Respuestas (2)

Walter Roberson
Walter Roberson el 5 de Oct. de 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 el 5 de Oct. de 2015
Editada: Stephen23 el 5 de Oct. de 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 -?

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by