How to limit input formats in an edit text box?

In my GUI (programmatic one) there is an edittext box, used to enter coeeficients.
I need to check the format of input and allow only to these types:
5
0.5
1.6
5.9
0.4565132
5.0000
00000.5
Then I'll save them and call back.

 採用された回答

Tom
Tom 2013 年 6 月 26 日

1 投票

STR2DOUBLE catches most of those things and outputs NaN if the number isn't valid. The only thing is that it will handle commas. I used REGEXPREP to switch any commas with asterisks so str2double can't process it.
validStr = {'5'
'0,5'
'1.0.6'
'5.9'
'0.4565132'
'5.0000'
'00000.5'};
validStr = regexprep(validStr,',','****');
str2double(validStr)

1 件のコメント

Ozan Oguz
Ozan Oguz 2013 年 6 月 27 日
This seems to work fine. Thank you!

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

その他の回答 (2 件)

Tom
Tom 2013 年 6 月 26 日

0 投票

validStr = {'5'
'0.5'
'1.6'
'5.9'
'0.4565132'
'5.0000'
'00000.5'};
if any(strcmp(get(hEditBox,'String'),validStr))
%code
else
warndlg('Edit box string is not valid.')
end

1 件のコメント

Ozan Oguz
Ozan Oguz 2013 年 6 月 26 日
Ok. I guess I cant explain it correctly.
It is like this:
There is a box to enter a coefficient (this can be any real number, and only one of it)
We can't use letters, comma or any other character (except numbers and a single dot, as in 0.04415 or 30.8)
Coefficient can be any real number in the correct format: Just numbers and only a single dot for decimals, if needed.
Thx.

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

Sean de Wolski
Sean de Wolski 2013 年 6 月 26 日

0 投票

Why not just use a popupmenu?
uicontrol('style','popupmenu','string',{'0.5','3.14','1.6'})

1 件のコメント

Ozan Oguz
Ozan Oguz 2013 年 6 月 26 日
Ok. I guess I cant explain it correctly.
It is like this:
There is a box to enter a coefficient (this can be any real number, and only one of it)
We can't use letters, comma or any other character (except numbers and a single dot, as in 0.04415 or 30.8)
Coefficient can be any real number in the correct format: Just numbers and only a single dot for decimals, if needed.
Thx.

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

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2013 年 6 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by