How to use 'for loop' to check user input?

9 ビュー (過去 30 日間)
Lauren-Xante Claassen
Lauren-Xante Claassen 2023 年 7 月 21 日
編集済み: VBBV 2023 年 7 月 21 日
I am trying to ask a user to input a value between 0-1. I then want to check that value against pre-existing variables r1b, r2b, r3b to see if it is greater and output the corresponding column as disp. So if user enters 0.15, it will be greater than r2b and r3b and the return will be columns r2a and r3a.
I have code to display the dialog box and return an error if not correct, but am not sure how to incorporate the loop?
r1a= column
r2a= colum
r3a= column
r1b=0.1
r2b=0.2
r3b=0.3
exit=false;
msg='Please enter an R^2 value for Cement:';
while ~exit
data = str2double( inputdlg(msg) );
exit = (0<= data1 && 1>=data1);
if ~exit
msg = 'Input must be between the values 0-1. Please re-enter: ';
end
end

採用された回答

Nandini
Nandini 2023 年 7 月 21 日
To check the user input against pre-existing variables and output the corresponding columns based on the condition, you can use a loop and conditional statements. Here's an example of how you can modify your code to achieve this:
r1a = column1;
r2a = column2;
r3a = column3;
r1b = 0.1;
r2b = 0.2;
r3b = 0.3;
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: ';
else
if data > r1b
disp(r1a);
end
if data > r2b
disp(r2a);
end
if data > r3b
disp(r3a);
end
end
end
I hope this helps! Let me know if you have any further questions.
  1 件のコメント
Lauren-Xante Claassen
Lauren-Xante Claassen 2023 年 7 月 21 日
Thank you Nandini!
I'm not sure I explained this as well as I could.
If the columns were from a Matrix (x), could I return the results in a new matrix?
x = 1 2 3
1 2 3
1 2 3
a =3
b= 6
c= 9
User input is 5.
loop statement
Return display:
y = 2 3
2 3
2 3

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

その他の回答 (1 件)

VBBV
VBBV 2023 年 7 月 21 日
編集済み: VBBV 2023 年 7 月 21 日
modify the upper limit in exit condition line according to the user input,
x = [ 1 2 3
1 2 3
1 2 3];
r1a = x(:,1);
r2a = x(:,2);
r3a = x(:,3);
r1b = 3;
r2b = 6;
r3b = 9;
exit = false;
msg = 'Please enter an R^2 value for Cement:'
msg = 'Please enter an R^2 value for Cement:'
if ~exit
data = str2double('5') % // e.g. user input
exit = (0 <= data && 10 >= data) % modify the upper limit in line according the user input
if ~exit
msg = 'Input must be between the values 0-1. Please re-enter: ';
end
if data > r1b & (data < r2b | data < r3b)
disp([r2a r3a])
elseif data > r2b & (data < r1b | data < r3b)
disp([r1a r3a])
elseif data > r3b & (data < r1b | data < r2b)
disp([r1a r2a])
end
end
data = 5
exit = logical
1
2 3 2 3 2 3
  1 件のコメント
VBBV
VBBV 2023 年 7 月 21 日
編集済み: VBBV 2023 年 7 月 21 日
you might want to add more conditions if the user input is less than all other values in conditions , in which case it need to prompt user for required output. Here i have used if-else conditional statement to demonstrate, since using a while loop it will never exit the loop, so its better to include a condition to exit the loop when the user inputs / outputs are all satisfied. Preferabl, a break statement with a condition inside the loop is necessary to exit the loop
x = [ 1 2 3
1 2 3
1 2 3];
r1a = x(:,1);
r2a = x(:,2);
r3a = x(:,3);
r1b = 0.1;
r2b = 0.2;
r3b = 0.3;
exit = false;
msg = 'Please enter an R^2 value for Cement:'
msg = 'Please enter an R^2 value for Cement:'
if ~exit
data = str2double('0.15') % // e.g. user input
exit = (0 <= data & 1 >= data) % modify the upper limit in line according the user input
if ~exit
msg = 'Input must be between the values 0-1. Please re-enter: ';
end
if data > r1b & (data > r2b & data > r3b)
disp('Specify the output required')
elseif data > r1b & (data < r2b & data < r3b)
disp([r2a r3a])
elseif data > r2b & (data < r1b & data < r3b)
disp([r1a r3a])
elseif data > r3b & (data < r1b & data < r2b)
disp([r1a r2a])
end
end
data = 0.1500
exit = logical
1
2 3 2 3 2 3

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

カテゴリ

Help Center および File ExchangeLoops 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