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

1 回表示 (過去 30 日間)
Yong Loong Chan
Yong Loong Chan 2019 年 3 月 12 日
コメント済み: Rik 2019 年 3 月 14 日
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 件のコメント
darova
darova 2019 年 3 月 12 日
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) )

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

採用された回答

Rik
Rik 2019 年 3 月 12 日
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 件のコメント
Rik
Rik 2019 年 3 月 14 日
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.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDialog Boxes についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by