I have a problem. I want to create a edit text and then when I type a string in it, it will display nothing like a empty. That is to say it allows only numbers. I found some ways but they use push button for checking. Please help me.
Thank you in advance.

 採用された回答

Jan
Jan 2016 年 3 月 31 日

0 投票

What does "only numbers" exactly mean? "1e3" is a number also.
What about checking the value inside the callback?
function Edit_Callback(hObject, EventData)
Str = get(hObject, 'String');
Value = sscanf(Str, '%g', 1);
fixStr = sprintf('%g', Value);
set(hObject, 'String', 'fixStr);
This tries to interprete the contents as 1 number and removes other characters.

2 件のコメント

Quan Tran
Quan Tran 2016 年 4 月 1 日
Thank you so much
But I have another problem :) When I typed '8.8*10^-7' in edit text, then I pushed enter button but it displayed '8.8'. How can I control it? I mean I want to type the mathematical symbol also.
Walter Roberson
Walter Roberson 2016 年 4 月 1 日
You need to define all of the possible number formats, and then you need to write code that parses them. For example do you also allow 8.8E-7 ? 8.8d-7 ? .5e-7 ? 5E+007 ? 8.8+5.2i ? 8.8^-7 (where it is not 10 which is the base being raised) ? 8.8•10^+7 ? 8.8*10^—7 (notice that is one of the dashes, not a minus sign) ? 8.8*10ˆ7 (notice that is ˆ not ^) ? 8.8·10^-7 ? 8.8×10^-7 ?

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

その他の回答 (3 件)

Daveed Daveed
Daveed Daveed 2022 年 7 月 31 日

0 投票

Hello. i was walking around here and i thought the following solution might be usefull for future people (may be the past time traveler :) )
function prevent_string(textbox_control)
if textbox_control.String ~= "" && isnan(str2double(textbox_control.String))
textbox_control.String = "";
end
Walter Roberson
Walter Roberson 2022 年 7 月 31 日

0 投票

These days you would use a
uieditfield('numeric')
which requires uifigure() (and will not work for traditional figures)

カテゴリ

ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索

製品

タグ

質問済み:

2016 年 3 月 31 日

回答済み:

2022 年 7 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by