Using while statements to create a loop if a value entered is not correct

7 views (last 30 days)
So im writing a code right now which prompts the user to enter the a number from 1-3. ive created a while statement which creates a loop and prompts them to type the value in again untill its either 1 2 or 3. However when i run the code and type in 2 for example, which is a correct number, its staying in the loop as if ive entered a wrong number. The only number which gets me out of the loop is when i use the number one. I feel like my while statement is incorrect, Here is the code: (ignore the MaxT and MinT)
case 1
MaxT = 0; MinT = 0;
disp('You have chosen average time to complete')
prompt = ['Please type which cube you would like\n<strong>Option 1:' ...
'</strong>2x2\n<strong>Option 2:</strong>3x3\n<strong>Option 3:</strong>4x4'];
cube_choice_avg_time = input(prompt);
while cube_choice_avg_time ~= 1;2;3;
prompt = ['You need to enter a value of 1,2 or 3\nPlease type ' ...
'which cube you would like\n<strong>Option 1:</strong>2x2\n<strong>Option 2:' ...
'</strong>3x3\n<strong>Option 3:</strong>4x4'];
cube_choice_avg_time = input(prompt);
end
if cube_choice_avg_time == 1
Full_cube_data = xlsread("Full_Cube_data.xlsx");
specific_data = Full_cube_data(73:108);
Avg = mean(specific_data);
disp(['Average Time (s) = ', num2str(Avg)])
elseif cube_choice_avg_time == 2
Full_cube_data = xlsread("Full_Cube_data.xlsx");
specific_data = Full_cube_data(37:72);
Avg = mean(specific_data);
disp(['Average Time (s) = ', num2str(Avg)])
elseif cube_choice_avg_time == 3
Full_cube_data = xlsread("Full_Cube_data.xlsx");
specific_data = Full_cube_data(1:36);
Avg = mean(specific_data);
disp(['Average Time (s) = ', num2str(Avg)])
end

Accepted Answer

Walter Roberson
Walter Roberson on 13 Feb 2021
while ~ismember(cube_choice_avg_time, 1:3)

More Answers (1)

Sharareh J
Sharareh J on 12 Feb 2021
Write [1 2 3] instead of 1;2;3

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!