How can i correctly write an if "yes or no" statement?

56 ビュー (過去 30 日間)
Kelvin Luo
Kelvin Luo 2017 年 5 月 26 日
回答済み: Salam Ismaeel 2019 年 11 月 28 日
For Example, x=input('Do you know the area of your building?(yes/no)' ,'s') if (x=='yes') area=input('What is the area of your building?') elseif (x=='no') shape=input('is your building circular or rectangular?') end How could i make it so that if the input is "yes" or "no" that it would do one of the following commands?

採用された回答

Jan
Jan 2017 年 5 月 26 日
編集済み: Jan 2017 年 5 月 26 日
if strcmpi(x, 'yes')
...
elseif strcmpi(x, 'no')
...
else
error('Please type "yes" or "no"');
end
The == is the elementwise comparison, which fails when x and 'yes' have a different number of characters.

その他の回答 (1 件)

Salam Ismaeel
Salam Ismaeel 2019 年 11 月 28 日
Just for genral use, not necessary for this case only. I create this function:
%******************************************************************************************
% CheckYesOrNo.m
% This function return 1 for Yes and 0 for No
% Input
% chos : charater variable
% Output:
% T : Logic variable (0 or 1)
%******************************************************************************************
function T = CheckYesOrNo(chos)
chos = lower(chos);
while chos ~= 'y' || chos ~= 'n'
if chos == 'y' || chos == 'n'
break
else
chos=input('Please input y or n? ','s');
end
end
if chos =='y'
T = 1;
else
T = 0;
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by