How to lock an enter field for an area of numbers and no letters.

1 回表示 (過去 30 日間)
Bianca Brusis
Bianca Brusis 2016 年 12 月 11 日
コメント済み: Star Strider 2016 年 12 月 12 日
Hello all, my question is how i can lock an area of numbers in the enter field which the user can only enter. Like i want that the user can only enter numbers in this field between for example 1-10 and only positivenumbers an no letters. Every ohter input should generate an error. :) Thank you.
  1 件のコメント
per isakson
per isakson 2016 年 12 月 11 日
With GUIDE that feature doesn't exist. The uicontrol outputs a character string and some code is needed to convert to numerical and test the value.
In R2016a (or b) App Designer the feature exists - I think.

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

回答 (2 件)

Isabella Osetinsky-Tzidaki
Isabella Osetinsky-Tzidaki 2016 年 12 月 11 日
if (x>10 || x<1)
error('User can only enter numbers between 1-10')
end
  2 件のコメント
Bianca Brusis
Bianca Brusis 2016 年 12 月 12 日
Hi, this works nearby. I want to enter in one field only numbers between 0 and 1 and when i enter 0.1 then everythink works fine when i enter 0,1 the programmcode use it like 1 but when i enter 0,2 the error occures. Any Idea?
Bianca Brusis
Bianca Brusis 2016 年 12 月 12 日
It seems like matlab ignores the , in the enter field.

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


Star Strider
Star Strider 2016 年 12 月 11 日
編集済み: Star Strider 2016 年 12 月 12 日
EDIT Changed to reflect different decimal separators. Now supports comma (,) and point (.) decimal separators. Checks for alphabetic inputs and for out-of-range numbers.
Try this:
nr_lims = [0 10]; % Number Limits
ltrs = sprintfc('%c', ['A':'Z' 'a':'z']); % Letters Array
respcell = inputdlg(sprintf('Enter a number between %.1f and %.1f ', nr_lims));
resp = respcell{:};
if (any(ismember(resp,ltrs)))
errordlg(sprintf('Input must be a number between %.1f and %.1f ', nr_lims),'Input Error')
return
else
parsenum = regexprep(resp, {'\.|\,'}, '.'); % Replace ‘,’ With ‘.’ In Number
num = str2num(parsenum); % Convert String To Double
if (num >= nr_lims(1)) && (num <= nr_lims(2)) % Check For Out-Of-Range
msgbox(sprintf('Your number is %f', num), 'Success!')
else
errordlg(sprintf('Input must be a number between %.1f and %.1f ', nr_lims),'Input Error')
return
end
end
  2 件のコメント
Bianca Brusis
Bianca Brusis 2016 年 12 月 12 日
wow :) I will try this! :) Thanks!
Star Strider
Star Strider 2016 年 12 月 12 日
My pleasure!

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

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by