필터 지우기
필터 지우기

Access Table rows if variable value == ... Problems with string variables.

조회 수: 2 (최근 30일)
roblocks
roblocks 2016년 6월 29일
댓글: roblocks 2016년 6월 29일
Dear All, suppose you have a table. I want to access certain rows based on variables values, for example if datecode == 20001231 & country_code == 'ES'. The following code works for numerical variables, but not for strings:
test = EBADATA_DEFINITIONS(EBADATA_DEFINITIONS.MATURITY_CODE == 999 ...
& EBADATA_DEFINITIONS.INFORMATION_CODE == 34010 ...
& strcmp(EBADATA_DEFINITIONS.C_COUNTRY_CODE,'ES'),:);
I have tried to manipulate the country column in various ways before running the command above , but I always end up with one or another error.
EBADATA_DEFINITIONS.C_COUNTRY_CODE = char(EBADATA_DEFINITIONS.C_COUNTRY_CODE)
EBADATA_DEFINITIONS.C_COUNTRY_CODE = cell2str(EBADATA_DEFINITIONS.C_COUNTRY_CODE)
Could someone point me towards the correct way of doing this?
I appreciate your help!
Best!

채택된 답변

Walter Roberson
Walter Roberson 2016년 6월 29일
It seems to work for me:
t = cell2table({15, 134, 'hello'; 82, 419, 'there'; 15, -999, 'hello'}, 'VariableNames', {'MATURITY_CODE', 'INFORMATION_CODE', 'C_COUNTRY_CODE'});
t(t.MATURITY_CODE == 15 & strcmp(t.C_COUNTRY_CODE, 'hello'),:)
  댓글 수: 1
roblocks
roblocks 2016년 6월 29일
it works for me now as well after not-charing it. I must have forgotten to unchar when I tested the code and then posted. Thanks Walter!

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by