Effacer les filtres
Effacer les filtres

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

5 vues (au cours des 30 derniers jours)
Lauren-Xante Claassen
Lauren-Xante Claassen le 21 Juil 2023
Modifié(e) : VBBV le 21 Juil 2023
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

Réponse acceptée

Nandini
Nandini le 21 Juil 2023
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 commentaire
Lauren-Xante Claassen
Lauren-Xante Claassen le 21 Juil 2023
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

Connectez-vous pour commenter.

Plus de réponses (1)

VBBV
VBBV le 21 Juil 2023
Modifié(e) : VBBV le 21 Juil 2023
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 commentaire
VBBV
VBBV le 21 Juil 2023
Modifié(e) : VBBV le 21 Juil 2023
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by