How to allow only the input of numeric data to uitable and text box?
Afficher commentaires plus anciens
Good morning. I have the following problem I have a uitable in main GUI that has "n" rows (the rows depend on the value that the user enters in a text box) and 10 columns. I require a code (something like a keypresskeypress event) that allows me to evaluate and discard the entered data that are not numeric both in the uitable and in some text boxes that I have in the main GUI. I do not know much about event management in MATLAB and I'm having a lot of problems with this. My uitable is called ("TablaDatosElementos") and the text box that allows creating the number of rows is called ("NumElem") Thank you very much and I hope you can help me.
6 commentaires
Walter Roberson
le 11 Juin 2019
Do you need to reject the invalid data as soon as the character is typed, or can it wait until the user finishes entry for that location ?
What characters are to be permitted in numeric entry? '-' for negative, '+' for positive, 0-9, '.', 'e' and 'E' and 'd' and 'D' for exponents, 'i' and 'I' and 'j' and 'J' and 'k' and 'K' for complex? Space permitted between the real part and complex part? How many digits of floating point exponent? Is it permitted to omit leading 0, such as -.5 ?
dpb
le 11 Juin 2019
And what happened with the last question on this GUI...you have yet to respond there before opening yet another... :(
Pedro Guevara
le 11 Juin 2019
Pedro Guevara
le 11 Juin 2019
Pedro Guevara
le 12 Juin 2019
Réponses (1)
Walter Roberson
le 12 Juin 2019
0 votes
Edit boxes (uicontrol):
You need to set a KeyPressFcn callback.
The first time KeyPressFcn fires after the control gets focus, you can reliably retrieve the String property to find out what is already there.
After that, the String property will not be updated until the control loses focus or the user presses enter, so you need to keep track of every character yourself, keeping an internal buffer of what you have seen and what the current state is. You can update the String property yourself, but it will still read back what it had when the control gained focus, but your changes will affect what is retrieved once focus loss / enter pressed.
With KeyPressFcn in place, the uicontrol will not handle backspace / delete itself: you will need to recognize the characters and adjust your internal buffer accordingly. Most people who do this do not bother to recognize the cursor keys to support positioning within the input.
You will need to keep track of the state: + and - only permitted as the first character, period only accepted once, must have at least one digit before or after the period or on both sides of the period, but period by itself is not valid.
5 commentaires
Walter Roberson
le 12 Juin 2019
uitable:
Although there is a KeyPressFcn callback for cells, the Event data that is received gives no indication about which cell you are positioned over.
The CellEditCallback only gets control after the user has pressed return or focus has moved to a different cell, and after MATLAB has already tested for compliance with the ColumnFormat property. CellEditCallback is fine custom handling after the user finishes entry, but not while the user is in the middle of entry.
To handle situations while the user is in the middle of typing, you might need to resort to the Java level.
Pedro Guevara
le 12 Juin 2019
Stephen23
le 12 Juin 2019
"Most people who do this do not bother to recognize the cursor keys to support positioning within the input."
... and anyway would not handle when the cursor is moved with the mouse.
Walter Roberson
le 12 Juin 2019
The link that I gave earlier is to documentation by the person who literally wrote the book on using Java with matlab.
Pedro Guevara
le 13 Juin 2019
Catégories
En savoir plus sur Entering Commands dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!