"if "function
이전 댓글 표시
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
채택된 답변
추가 답변 (3개)
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
vijayasinthujan vijayaratnam
2012년 2월 19일
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
2012년 2월 19일
Simply use "help ischar" and "help isnumeric".
Walter Roberson
2012년 2월 19일
0 개 추천
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 :(
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!