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

2 次查看(过去 30 天)
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