MATLAB Answers


How to allow only the input of numeric data to uitable and text box?

Pedro Guevara さんによって質問されました 2019 年 6 月 11 日
最新アクティビティ Pedro Guevara さんによって コメントされました 2019 年 6 月 13 日
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 件のコメント

Pedro Guevara 2019 年 6 月 11 日
I require that the input and recognition of characters be done immediately (as is achieved with a keypress in visual basic) and I require that they be only numeric characters, with a decimal point, with signs of + and / or -. I do not require anything else. Thank you very much for your help.
Pedro Guevara 2019 年 6 月 12 日
I read this documentation and many things that are beyond my understanding. How can I use it to use it? Is not there a way to do it with the tools of MATLAB? Thank you.

サインイン to comment.




1 件の回答

Walter Roberson
回答者: Walter Roberson
2019 年 6 月 12 日

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 件のコメント

Stephen Cobeldick
2019 年 6 月 12 日
"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
2019 年 6 月 12 日
The link that I gave earlier is to documentation by the person who literally wrote the book on using Java with matlab.
Pedro Guevara 2019 年 6 月 13 日
Hello again. Thinking about it, maybe I do not require that the validation be done "immediately", it can be given in such a way that when passing from one cell to another (of my uitable "TableDataElements") the validation action is executed, erasing from that first cell the characters that will not be numeric, and as I understand this can be achieved with the "CellEditCallback" function, but I do not know how to create that code. Maybe this way you do not have to resort to JAVA tools for code generation. Under this new condition I do not know if it is easier for you to help me with the problem. Thank you.

サインイン to comment.

Translated by