How can I show the error message when the input data get wrong?

조회 수: 5 (최근 30일)
Sin Man
Sin Man 2022년 12월 11일
편집: Sin Man 2022년 12월 11일
I want to create the inputdlg to let user to input the data. But how can I add the check coding to check the input is between 2000 and 2019? Thank you.
My current coding:
prompt = {'Start Year:','End Year:'};
dlgtitle = 'Year Range (Between 2000 and 2019)';
dims = [1 50];
answer = inputdlg(prompt,dlgtitle,dims);
disp(answer)

채택된 답변

VBBV
VBBV 2022년 12월 11일
편집: VBBV 2022년 12월 11일
prompt = {'Start Year:','End Year:'};
dlgtitle = 'Year Range (Between 2000 and 2019)';
dims = [1 50];
answer = inputdlg(prompt,dlgtitle,dims);
if str2num(answer{1}) < 2000 | str2num(answer{2}) > 2019 % check the condition for year range
disp('Error: please enter valid range')
else
disp(answer)
end
  댓글 수: 1
Sin Man
Sin Man 2022년 12월 11일
편집: Sin Man 2022년 12월 11일
Thank You. If I also want to show the error when the input is not a integer. How can I do?

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

추가 답변 (1개)

KSSV
KSSV 2022년 12월 11일
prompt = {'Start Year:','End Year:'};
dlgtitle = 'Year Range (Between 2000 and 2019)';
dims = [1 50];
answer = inputdlg(prompt,dlgtitle,dims);
while any(str2double(answer)<2019) || any(str2double(answer) > 2020)
answer = inputdlg(prompt,dlgtitle,dims);
end
disp(answer)
  댓글 수: 1
Sin Man
Sin Man 2022년 12월 11일
편집: Sin Man 2022년 12월 11일
If I also want to show the error when the input is not a integer (such as the input is other symbol or wording). How can I do?
Thank you

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by