Error using == function when trying to group certain data

조회 수: 1 (최근 30일)
MZINGAYE MUBAYA
MZINGAYE MUBAYA 2019년 2월 19일
댓글: MZINGAYE MUBAYA 2019년 2월 21일
Im trying to run a code to create a subset of a group i.e females who are credit worthy but im struggling with getting program to recognize f for females
>> worthyf = credit(credit.gender=='f' & credit.Risk_class==1,:);
Undefined operator '==' for input arguments of type 'cell'.

채택된 답변

Jesus Sanchez
Jesus Sanchez 2019년 2월 19일
== only works for numbers. You are comparing strings and therefore you need to use "strcmp"
An example in your code:
worthyf = credit(strcmp(credit.gender,'f') & credit.Risk_class==1,:);
  댓글 수: 5
Guillaume
Guillaume 2019년 2월 20일
Actually, I completely forgot the most relevant class here, the newish string class. This has an overloaded == operator that is equivalent to strcmp. Unfortunately, that's going to lead to lot of confusion for beginner because indeed == doesn't work as they expect for char arrays (which unfortunately many people call strings) but does work for actual strings.
s = "somethingelse"; %an actual matlab string
if s == "something"
disp('it''s something');
elseif s == "somethingelse"
disp('it''something else');
end
works as expected
s = 'something else'
if s == 'something' %proper syntax: if strcmp(s, 'something')
disp('it''s something');
elseif s == 'somethingelse'
disp('it''something else');
end
does not work. you have to use strcmp. strcmp also works for actual strings, so it may be safer to always use that.
MZINGAYE MUBAYA
MZINGAYE MUBAYA 2019년 2월 21일
So basically use strcmp for my data set

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by