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 CenterFile Exchange에서 Dialog Boxes에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by