how to display 'Not a number, try again'

조회 수: 2 (최근 30일)
Cross Category
Cross Category 2020년 4월 24일
댓글: Cross Category 2020년 4월 24일
How do I display 'not a number, try again' when a word has been entered (eg. the letter a), without prducing error messages?
x = input('type a number: ')
t=isstring(x)
while t =1
disp('Not a number, try again')
x = input('type a number: ')
end
y = x*3
disp(y)

채택된 답변

Walter Roberson
Walter Roberson 2020년 4월 24일
msg = 'type a number: ';
while true
xs = input(msg, 's');
x = str2double(xs);
if ~isnan(x); break; end
disp('Not a number, try again')
end
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 4월 24일
; is a statement separator that indicates that the expression before it is to be executed and any default display of results is not to be done. You can can also use comma instead, and when you do, the default output will appear.
There is a completely different use as well. Inside [] or {} indicates that a new row of values is to be started, such as [1 2;3 4] to define the array
1 2
3 4
Cross Category
Cross Category 2020년 4월 24일
Thank you so much, right after I asked the question, I realised I would google it. So I deleted the question.
But thank you for the detailed explaination.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by