How do I prompt user to re-input value if first attempt is incorrect?

조회 수: 10 (최근 30일)
I am trying to prompt user to input a value for R^2, accepting or rejecting value within a range 0-1. If value is correct, proceed to next input question. If value is incorrect, display error message to user and re-prompt them to insert new value within range. Currently, I get the error but it does not allow me to re-enter a new value for that variable, it simply continues to next prompt. I need to create a loop of some kind but am not sure how?
prompt = inputdlg('Please enter an R^2 value for Cement:');
data = str2double(prompt);
if 0<= data && 1>=data
else
errordlg('Input must be between the values 0-1.');
end
prompt = inputdlg('Please enter an R^2 value for Blast Furnace:');
data = str2double(prompt);
if 0<= data && 1>=data
else
errordlg('Input must be between the values 0-1.');
return
end

채택된 답변

Matt J
Matt J 2023년 7월 19일
편집: Matt J 2023년 7월 19일
Something like this, perhaps?
exit=false;
msg='Please enter an R^2 value for Cement: '
while ~exit
data = str2double( inputdlg(msg) );
exit = (0<= data && 1>=data);
if ~exit
msg = 'Input must be between the values 0-1. Please re-enter: ';
end
end
  댓글 수: 2
Lauren-Xante Claassen
Lauren-Xante Claassen 2023년 7월 19일
Hi Matt,
This seems to be displaying the error even when I enter in correct values?
Lauren-Xante Claassen
Lauren-Xante Claassen 2023년 7월 19일
Thanks a million - it finally works!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by