フィルターのクリア

How to accept only numbers in this case?

5 ビュー (過去 30 日間)
Jose Antonio  Salcedo Simon
Jose Antonio Salcedo Simon 2017 年 12 月 15 日
Hello, I want to improve the text box "caja_lonsat" by forcing that only numbers can be inserted, how could I do it? Thanks for this.
---------------------------
global caja_lonsat lonsat
caja_lonsat= uicontrol('style','edit','Units','normalized','pos',tex(29,:),...
'ForegroundColor','black','String',num2str(lonsat,'%4.2f'),...
'Horizontalalignment','left',...
'Backgroundcolor','white','Fontsize',12);
lonsat=str2num(get(caja_lonsat,'string'));
---------------------------
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 12 月 18 日
When you say that only numbers can be inserted, do you mean only the digits '0' to '9', or do you include positive and negative signs, and do you include decimal points, and do you include exponential floating point format, and do you include complex numbers?
Jose Antonio  Salcedo Simon
Jose Antonio Salcedo Simon 2017 年 12 月 18 日
Hi Walter, thanks for your answer.
I only want to insert digits of '0' to '9', negative signs, and the decimal point.

サインインしてコメントする。

回答 (2 件)

KL
KL 2017 年 12 月 15 日
use uieditfield
fig = uifigure;
edt = uieditfield(fig,'numeric');
  1 件のコメント
Jose Antonio  Salcedo Simon
Jose Antonio Salcedo Simon 2017 年 12 月 18 日
Hi KL,
Thank you very much for your answer, unfortunately my version available is 2009 and I cant use this sentence. Any other idea?

サインインしてコメントする。


Walter Roberson
Walter Roberson 2017 年 12 月 18 日
In order to accept only a limited set of characters in a uicontrol style edit, you need to use the uicontrol KeyPressFcn callback. The callback needs to look in its eventdata to determine which key was pressed. If the key pressed is to be rejected, the callback must change the String property to remove the key.
Now, there is a difficulty involved: when you use a KeyPressFcn callback, then when you fetch the String property, even though some internal buffer is seeing the keystrokes, the String property returned will only reflect what was in the String property as of the time the user clicked in the field to start editing, and the String property will not be properly updated until the user pressed Return on the field or the user clicks outside the field.
It can be tricky to properly use both the KeyPressFcn and the Callback on the same field.
You will probably need to keep track of deletions as well. You will need to keep a buffer of keys outside of the String property and you will need to detect the return or newline and set the String property at that time with the validated data. Oh, and notice that the display may not be updated as you type...
All in all, it is usually much easier to instead wait until the user has typed in the entire field and then check to see if what they typed in is numeric, by fetching the String property in the Callback function, and using str2double(), and testing to see if that came out NaN. In other words, it is much easier to not prevent anything non-numeric from being entered, and to instead complain after the user has completed entering the non-numeric item.
  1 件のコメント
Jose Antonio  Salcedo Simon
Jose Antonio Salcedo Simon 2017 年 12 月 18 日
Hi again,
I see perfect to check later if the entry is numeric, I am a little new, could you guide me a bit in the use of the Callback function, and the post complain?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by