"if "function

24 ビュー (過去 30 日間)
vijayasinthujan vijayaratnam
vijayasinthujan vijayaratnam 2012 年 2 月 19 日
編集済み: Jan 2013 年 10 月 16 日
how to write "IF" function to only accept numbers NO letters
need to ask input number (if user type any letters need to ask again to give number)
i know basic IF function but i need to know how to verified the input as number or letter ?
thank you Vijay

採用された回答

Jiro Doke
Jiro Doke 2012 年 2 月 19 日
You can use the isstrprop function. If answer is the input you got from your user (using the input command),
all(isstrprop(answer, 'digit'))
Here's an example:
while true
answer = input('Enter a number: ', 's');
if all(isstrprop(answer, 'digit'))
answer = str2double(answer);
break;
else
disp('Only numbers supported');
end
end
Here's another way. This way is slightly better because it allows decimal points:
while true
answer = input('Enter a number: ', 's');
answer = str2double(answer);
if ~isnan(answer)
break;
else
disp('Only numbers supported');
end
end
  2 件のコメント
vijayasinthujan vijayaratnam
vijayasinthujan vijayaratnam 2012 年 2 月 19 日
100% it is working
Jan
Jan 2012 年 2 月 19 日
The first method isstrprop(x,'digit') rejects the decimal point also, while the 2nd method accepts inputs as "3.14". In addition "1e3" should be accepted also, such that I prefer the STR2DOUBLE method.

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

その他の回答 (3 件)

Wayne King
Wayne King 2012 年 2 月 19 日
Hi Vijay
if isletter(input)
disp('Please enter a number, not a letter');
end
  1 件のコメント
vijayasinthujan vijayaratnam
vijayasinthujan vijayaratnam 2012 年 2 月 19 日
hi Wayne king
i knew that but if the user type mistake, then he/she need to re-start the program(need to input more detail from begin, this can make him bored )

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


Image Analyst
Image Analyst 2012 年 2 月 19 日
Did you try using ischar() and isnumeric()? Or try something like this, or similar:
% Ask user for a number.
defaultValue = 45;
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, 'Enter the numeric value',1,{num2str(defaultValue)});
integerValue = round(str2num(cell2mat(caUserInput)));
% Check for a valid integer.
if isempty(integerValue)
% They didn't enter a number.
% They entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
  3 件のコメント
Image Analyst
Image Analyst 2012 年 2 月 19 日
They are built in functions to check if the argument is a character or a number. Are they not in your help?
Jan
Jan 2012 年 2 月 19 日
Simply use "help ischar" and "help isnumeric".

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


Walter Roberson
Walter Roberson 2012 年 2 月 19 日
In MATLAB, "if" is not a function: it is a control statement.
We've been nagging MathWorks for years to create a function equivalent to "if", but with no success yet :(
  1 件のコメント
vijayasinthujan vijayaratnam
vijayasinthujan vijayaratnam 2012 年 2 月 19 日
ok

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

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by