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

1 vue (au cours des 30 derniers jours)
Yong Loong Chan
Yong Loong Chan le 12 Mar 2019
Commenté : Rik le 14 Mar 2019
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 commentaire
darova
darova le 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) )

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 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 commentaire
Rik
Rik le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dialog Boxes dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by