How to limit input formats in an edit text box?
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Tom
2013-6-26
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)
更多回答(2 个)
Tom
2013-6-26
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
Sean de Wolski
2013-6-26
Why not just use a popupmenu?
uicontrol('style','popupmenu','string',{'0.5','3.14','1.6'})
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!