How to progress from one user input to the next?

2 ビュー (過去 30 日間)
Lauren-Xante Claassen
Lauren-Xante Claassen 2023 年 5 月 15 日
コメント済み: Cris LaPierre 2023 年 5 月 15 日
I am trying to move through a series of user inputs, e.g.:
Q1 - Please enter an R^2 value for Cement: value must be within numerical range.
If they answer incorrectly, I want it to display an error: ('Input must be between the values 0-1.').
If they answer correctly, I want a message that says: Value acceptedand then to to move on to the next question (user input).
When they have completed the last question, I want it to display a message that says: Operation Completed, so they know they have finished.
Below is the code I have, the problem is, if they enter an incorrect value the error appears but the next user input appears over and begins. How do I amend my code to create a waterfall effect through the questions please?
% Prompt user to input a value for r^2 for all ingredients, accepting or rejecting value within a range. Display message to user.
prompt = inputdlg('Please enter an R^2 value for Cement:');
data = str2double(prompt);
if 0<= data & 1>=data;
f = msgbox("Value accepted");
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;
f = msgbox("Value accepted");
else;
errordlg('Input must be between the values 0-1.');
end;
prompt = inputdlg('Please enter an R^2 value for Fly Ash:');
data = str2double(prompt);
if 0<= data & 1>=data;
f = msgbox("Operation Completed");
else;
errordlg('Input must be between the values 0-1.');
end;

回答 (1 件)

Cris LaPierre
Cris LaPierre 2023 年 5 月 15 日
An error dialog does not terminate code execution.
You might consider using error instead. If you prefer the error appearing in a dialog box, then you can add a return command after errordlg to terminate code execution.
  3 件のコメント
Lauren-Xante Claassen
Lauren-Xante Claassen 2023 年 5 月 15 日
If I use the error command it stops to display error and does not allow user to return to input correct variable and continue through prompts?
Cris LaPierre
Cris LaPierre 2023 年 5 月 15 日
Yes, that aligns with what I said.
I tested by running the script in the editor. You are testing in the command window. The behavior is different. Try working in a script instead of the command window.

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

カテゴリ

Help Center および File ExchangeTesting Frameworks についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by