if statement - working with characters

I am executing the following code:
x = inputdlg({'Month', 'Population renewal', 'Death rate'}, 'Inputs')
f = getfield(x,{1});
if (f == 'January')
b = 0.5
else
b = 0.7
end
and I am getting an error that says "??? Undefined function or method 'eq' for input arguments of type 'cell'"
Please do tell me where I am going wrong....

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 23 日

7 投票

Use
if isequal(f{1} ,'january')

7 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 23 日
[ Ojaswita commented]
Thanks alot for the answer... can you pls explain the logic too so that I can always have it in mind for next time... :)
And also what if I want to say that if f is january, february or march?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 23 日
f={'january'} % is a cell array
f{1} % is char
'jenuery'==f{1} % the result is the comparison of each character,
isequal('january',f{1}) will compare the two words, the result is 0 or 1
%for you last question
ismember(f,{'janury','march'})
Ojaswita
Ojaswita 2013 年 9 月 24 日
Thank you very very much! I understand it well and it works! :) :)
Jan
Jan 2013 年 9 月 24 日
編集済み: Jan 2013 年 9 月 24 日
For the comparison of strings, I'd prefer strcmp. This would even accept a cell string as input:
if strcmp(f, 'January')
Or
if strcmp(f{1}, 'January')
Ojaswita
Ojaswita 2013 年 9 月 24 日
It works equally well with strcmp... I tried strcmp before too but i didnt know how to insert my 'or' requirement. As in if f = january/february/march etc etc...
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 24 日
any(strcmp(f,{'janury','march'}))
Gwangil Mun
Gwangil Mun 2022 年 11 月 26 日
good

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by