Please advise me on how to improve the efficiency of this code?

1 view (last 30 days)
As the title says, how can i improve on this on the code on the second line?
try_again = input('Do you want to try again?\nPlease enter yes or no:','s');
while strcmp(try_again,'') == 1 | strcmp(try_again,'yes') ~= 1 | strcmp(try_again,'y') ~= 1 |strcmp(try_again,'no') ~= 1 | strcmp(try_again,'n') ~= 1
try_again = input('Please enter yes or no:','s');
if strcmp(try_again,'yes') | strcmp(try_again,'y')
scriptA
elseif strcmp(try_again,'no') | strcmp(try_again,'n')
break
end
end
Any help or tips would be very greatful, thank you very much
  1 Comment
darova
darova on 12 Mar 2019
while strcmp(try_again,'') == 1 | strcmp(try_again,'yes') ~= 1 | strcmp(try_again,'y') ~= 1 |strcmp(try_again,'no') ~= 1 | strcmp(try_again,'n') ~= 1
while isempty( strfind('yesno',try_again) )

Sign in to comment.

Accepted Answer

Rik
Rik on 12 Mar 2019
You could use an explicit dialog box to avoid any confusion:
answer = questdlg(...
'Do you want to try again?', ...%question body
'Retry?', ...%box title
'Yes','No', ...%options
'Yes');%default
if strcmp(answer,'Yes')
scriptA
end
If a GUI option is not acceptable you can use something similar to the suggestion by darova:
options={'yes',1;'y',1;'no',0;'n',0};
try_again = input('Do you want to try again?\nPlease enter yes or no:','s');
L=ismember(options(:,1),try_again);
while ~any( L )
try_again = input('Do you want to try again?\nPlease enter yes or no:','s');
L=ismember(options(:,1),try_again);
end
if options{L,2}
scriptA
end
  1 Comment
Rik
Rik on 14 Mar 2019
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.

Sign in to comment.

More Answers (0)

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!